-1
我有詳細介紹有關包含單詞「紅粉」帶信息文本的該塊(從Discogs API)......如何用PHP正確解析這段文字?我中途有
我試圖找出如何從這個文本塊中正確地提取藝術家名字。我的嘗試是:
<?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);
的最終目標是能夠呈現所提取的帶標題的列表。
任何洞察讚賞。謝謝。
謝謝你,看起來很有希望。 – matthew
對不起蟲...你碰巧知道爲什麼這只是不打印我的網頁上的任何東西?在「curl_close($ ch);」之後,我把「$ myArray = json_decode($ output,true); echo $ myArray [0] [」title「];」(不含引號)。就像我說的,它不會在我的網站上創建任何文本,並且我看到Chrome開發者控制檯沒有錯誤。 – matthew
@matthew數據不包含索引0的元素,在這裏看到(我複製粘貼與您的數據):https://eval.in/496633你可能想'$ myarray的[「結果」] [0] [ '標題']' – ArSeN