PHP simpleXML: parse specific attribute value of multiple entries

Viewed 364

I have the following XML:

<doc>
    <str name="segment">20170913064727</str>
    <str name="digest">427522d5ceb605f87755c909deac698a</str>
    <str name="title">AMS Site</str>
<doc>

I want to parse it and to save the value of "title" to the variable $ergTitle (that would be "AMS Site").

I have the following PHP code:

foreach($data->result->doc as $dat) {
    foreach ($dat->str as $att) {
        foreach ($att->attributes() as $a => $b) {
            echo $a,'="',$b,"\"\n" . '<br>';
            if ($b=='title') {
                echo "OK";
            }
        }
    }
}

Which results to:

name="segment" 
name="digest" 
name="id" 
name="title" 
OKname="url" 
name="content" 

But how can I now get the value of name="title" and save it to my variable?

Goal: I want to print the content of "content", "url", "title" and so on (its for a result site of a search query.)

2 Answers
Related