如何在PHP中解析信息?

时间:2010-09-13 06:32:51

标签: php json parsing

我在下面有以下文字,解析它的最简单方法是什么?

if(typeof sa_inst.apiCB == 'function') sa_inst.apiCB({"AS":{"Query":"g","FullResults":1,"Results":[{"Type":"AS","Suggests":[{"Txt":"g<strong>oogle</strong>","Type":"AS","Sk":""},{"Txt":"g<strong>mail</strong>","Type":"AS","Sk":"AS1"},{"Txt":"g<strong>oogle</strong> <strong>maps</strong>","Type":"AS","Sk":"AS2"},{"Txt":"g<strong>oogle</strong> <strong>earth</strong>","Type":"AS","Sk":"AS3"},{"Txt":"g<strong>ames</strong>","Type":"AS","Sk":"AS4"},{"Txt":"g<strong>oogle</strong> <strong>images</strong>","Type":"AS","Sk":"AS5"},{"Txt":"g<strong>amestop</strong>","Type":"AS","Sk":"AS6"},{"Txt":"g<strong>rainger</strong>","Type":"AS","Sk":"AS7"}]}]}} /* pageview_candidate */);

我想只得到后面的内容(“Txt”:)并且不包括<strong>或其他任何东西。例如,在上面的文字中我希望得到类似的内容:

google,gmail,google地图,google图片等

在一个数组中。请帮忙吗?

2 个答案:

答案 0 :(得分:6)

文字是JSON。使用json_decode进行解析。

$str = '{"AS":{"Query":"g","FullResults":1,"Results":[{"Type":"AS","Suggests":[{"Txt":"g<strong>oogle</strong>","Type":"AS","Sk":""},{"Txt":"g<strong>mail</strong>","Type":"AS","Sk":"AS1"},{"Txt":"g<strong>oogle</strong> <strong>maps</strong>","Type":"AS","Sk":"AS2"},{"Txt":"g<strong>oogle</strong> <strong>earth</strong>","Type":"AS","Sk":"AS3"},{"Txt":"g<strong>ames</strong>","Type":"AS","Sk":"AS4"},{"Txt":"g<strong>oogle</strong> <strong>images</strong>","Type":"AS","Sk":"AS5"},{"Txt":"g<strong>amestop</strong>","Type":"AS","Sk":"AS6"},{"Txt":"g<strong>rainger</strong>","Type":"AS","Sk":"AS7"}]}]}}';

$result = json_decode($str);

//Show all 'Txt' values
foreach($result->AS->Results[0]->Suggests as $suggests)
echo "Txt contains: ".htmlspecialchars($suggests->Txt). "<br/>";

//Show full contents of result
echo "<pre>";
print_r($result);

首先输出Txt的值:

Txt contains: g<strong>mail</strong>  
Txt contains: g<strong>oogle</strong> <strong>maps</strong>  
Txt contains: g<strong>oogle</strong> <strong>earth</strong>  
Txt contains: g<strong>ames</strong>  
Txt contains: g<strong>oogle</strong> <strong>images</strong>  
Txt contains: g<strong>amestop</strong>  
Txt contains: g<strong>rainger</strong>  

然后是完整的JSON数据:

stdClass Object
(
    [AS] => stdClass Object
        (
            [Query] => g
            [FullResults] => 1
            [Results] => Array
                (
                    [0] => stdClass Object
                        (
                            [Type] => AS
                            [Suggests] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [Txt] => google
                                            [Type] => AS
                                            [Sk] => 
                                        )

                                    [1] => stdClass Object
                                        (
                                            [Txt] => gmail
                                            [Type] => AS
                                            [Sk] => AS1
                                        )

                                    [2] => stdClass Object
                                        (
                                            [Txt] => google maps
                                            [Type] => AS
                                            [Sk] => AS2
                                        )

                                    [3] => stdClass Object
                                        (
                                            [Txt] => google earth
                                            [Type] => AS
                                            [Sk] => AS3
                                        )

                                    [4] => stdClass Object
                                        (
                                            [Txt] => games
                                            [Type] => AS
                                            [Sk] => AS4
                                        )

                                    [5] => stdClass Object
                                        (
                                            [Txt] => google images
                                            [Type] => AS
                                            [Sk] => AS5
                                        )

                                    [6] => stdClass Object
                                        (
                                            [Txt] => gamestop
                                            [Type] => AS
                                            [Sk] => AS6
                                        )

                                    [7] => stdClass Object
                                        (
                                            [Txt] => grainger
                                            [Type] => AS
                                            [Sk] => AS7
                                        )

                                )

                        )

                )

        )

)

答案 1 :(得分:0)

这会有效,但我删除了'from'function'

<?php
    echo $text = 'if(typeof sa_inst.apiCB == function sa_inst.apiCB({"AS":{"Query":"g","FullResults":1,"Results":[{"Type":"AS","Suggests":[{"Txt":"g<strong>oogle</strong>","Type":"AS","Sk":""},{"Txt":"g<strong>mail</strong>","Type":"AS","Sk":"AS1"},{"Txt":"g<strong>oogle</strong> <strong>maps</strong>","Type":"AS","Sk":"AS2"},{"Txt":"g<strong>oogle</strong> <strong>earth</strong>","Type":"AS","Sk":"AS3"},{"Txt":"g<strong>ames</strong>","Type":"AS","Sk":"AS4"},{"Txt":"g<strong>oogle</strong> <strong>images</strong>","Type":"AS","Sk":"AS5"},{"Txt":"g<strong>amestop</strong>","Type":"AS","Sk":"AS6"},{"Txt":"g<strong>rainger</strong>","Type":"AS","Sk":"AS7"}]}]}} /* pageview_candidate */);';
    echo '<br />';
    $text = strip_tags($text); // remove html tags
    $a = explode('"Txt":"', $text);
    $app = array();
    foreach ($a as $c) {
        $b = explode('"', $c);
        $app = $b[0];
    }
    unset($app[0]);
    var_dump($app);
?>