tags:

views:

82

answers:

2

I have the following text below, what is the easiest way to parse through it?

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 */);

I would like to only get what is after ("Txt":) and not including the <strong> or anything else. For example, form the text above I would like to get something like:

google, gmail, google maps, google images, etc

in an array. Help please?

+6  A: 

The text is JSON. Use json_decode to parse it.

$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);

This outputs values of Txt first:

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>  

And then the complete JSON data too:

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
                                        )

                                )

                        )

                )

        )

)
shamittomar
A: 

This will work, but I removed ' 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);
?>
Wolfy