如何使用 PHP 正确解析此文本?我到一半了
How to correctly parse this text with PHP? I'm halfway there
我有这个文本块(来自 Discogs API)详细介绍了包含单词 "Pink"...
的乐队的信息
我正在尝试弄清楚如何从这段文本中正确提取艺术家姓名。我的尝试是:
<?php
$url = "https://api.discogs.com/database/search?type=artist&q=pink"; // add the resource info to the url. Ex. releases/1
//initialize the session
$ch = curl_init();
//Set the User-Agent Identifier
curl_setopt($ch, CURLOPT_USERAGENT, 'YourSite/0.1 +http://your-site-here.com');
//Set the URL of the page or file to download.
curl_setopt($ch, CURLOPT_URL, $url);
//Ask cURL to return the contents in a variable instead of simply echoing them
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//Execute the curl session
$output = curl_exec($ch);
//close the session
curl_close ($ch);
function textParser($text, $css_block_name){
$end_pattern = '], "';
switch($css_block_name){
# Add your pattern here to grab any specific block of text
case 'title';
$end_pattern = '", "';
break;
}
# Name of the block to find
$needle = "\"{$css_block_name}\":";
# Find start position to grab text
$start_position = stripos($text, $needle) + strlen($needle);
$text_portion = substr($text, $start_position, stripos($text, $end_pattern, $start_position) - $start_position + 1);
$text_portion = str_ireplace("[", "", $text_portion);
$text_portion = str_ireplace("]", "", $text_portion);
return $text_portion;
}
$blockTitle = textParser($output, 'title');
echo $blockTitle. '<br/>';
?>
但这会导致此错误:
Warning: stripos(): Offset not contained in string in C:\xampp\htdocs\WellItsFixed3\TTpage1.php on line 41
第 41 行是
$text_portion = substr($text, $start_position, stripos($text, $end_pattern, $start_position) - $start_position + 1);
最终目标是能够在列表中显示提取的乐队名称。
任何见解表示赞赏。谢谢。
这显然是一个 JSON 编码字符串,您的方法超出了范围。只是做:
$data = json_decode($your_string);
和$data
将以结构化的方式包含所有信息,请参阅the json_decode() manual了解更多详情。
我有这个文本块(来自 Discogs API)详细介绍了包含单词 "Pink"...
的乐队的信息我正在尝试弄清楚如何从这段文本中正确提取艺术家姓名。我的尝试是:
<?php
$url = "https://api.discogs.com/database/search?type=artist&q=pink"; // add the resource info to the url. Ex. releases/1
//initialize the session
$ch = curl_init();
//Set the User-Agent Identifier
curl_setopt($ch, CURLOPT_USERAGENT, 'YourSite/0.1 +http://your-site-here.com');
//Set the URL of the page or file to download.
curl_setopt($ch, CURLOPT_URL, $url);
//Ask cURL to return the contents in a variable instead of simply echoing them
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//Execute the curl session
$output = curl_exec($ch);
//close the session
curl_close ($ch);
function textParser($text, $css_block_name){
$end_pattern = '], "';
switch($css_block_name){
# Add your pattern here to grab any specific block of text
case 'title';
$end_pattern = '", "';
break;
}
# Name of the block to find
$needle = "\"{$css_block_name}\":";
# Find start position to grab text
$start_position = stripos($text, $needle) + strlen($needle);
$text_portion = substr($text, $start_position, stripos($text, $end_pattern, $start_position) - $start_position + 1);
$text_portion = str_ireplace("[", "", $text_portion);
$text_portion = str_ireplace("]", "", $text_portion);
return $text_portion;
}
$blockTitle = textParser($output, 'title');
echo $blockTitle. '<br/>';
?>
但这会导致此错误:
Warning: stripos(): Offset not contained in string in C:\xampp\htdocs\WellItsFixed3\TTpage1.php on line 41
第 41 行是
$text_portion = substr($text, $start_position, stripos($text, $end_pattern, $start_position) - $start_position + 1);
最终目标是能够在列表中显示提取的乐队名称。
任何见解表示赞赏。谢谢。
这显然是一个 JSON 编码字符串,您的方法超出了范围。只是做:
$data = json_decode($your_string);
和$data
将以结构化的方式包含所有信息,请参阅the json_decode() manual了解更多详情。