Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PHP正确解析此文本?I';我已经走到一半了_Php_Function_Parsing_Text_Discogs Api - Fatal编程技术网

如何使用PHP正确解析此文本?I';我已经走到一半了

如何使用PHP正确解析此文本?I';我已经走到一半了,php,function,parsing,text,discogs-api,Php,Function,Parsing,Text,Discogs Api,我有这段文字(来自Discogs API)详细介绍了包含“粉色”一词的乐队信息 我正试图找出如何正确地从这段文字中提取艺术家的名字。我的尝试是: <?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

我有这段文字(来自Discogs API)详细介绍了包含“粉色”一词的乐队信息

我正试图找出如何正确地从这段文字中提取艺术家的名字。我的尝试是:

<?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/>';



?> 
最终目标是能够在列表中显示提取的乐队名称


任何见解都值得赞赏。谢谢。

这显然是一个JSON编码的字符串,您的方法太过火了。只要做:

$data = json_decode($your_string);

$data
将以结构化的方式包含所有信息,请参阅以了解更多详细信息。

谢谢,这看起来很有希望。很抱歉打扰您。。。你知道为什么这不能在我的页面上打印任何东西吗?在“curl_close($ch);”之后,我将“$myArray=json_decode($output,true);echo$myArray[0][“title”];”(不带引号)。正如我所说,它没有在我的网站上创建任何文本,我在Chrome开发者控制台中也没有看到任何错误。@matthew数据不包含索引为0的元素,请参见此处(我复制并粘贴了您的数据):您可能想获得
$myArray['results'][0]['title']
$data = json_decode($your_string);