I have an rss feed that I'd like to parse. Everything is working fine but I'm not getting the value of the <enclosure>tag. In the feed it looks like this: <enclosure url="http://www.example.com/mp3s/theOtherOne.mp3" length="6666097" type="audio/mpeg"/> Code (markup): My question is: How do I get the value of the <enclosure> tag, i.e. the url?
You can use this <?php $s = ' <junk url="aaa" length="213123" /> <enclosure url="http://www.example.com/mp3s/theOtherOne.mp3" length="6666097" type="audio/mpeg"/> <enclosure url="http://www.example2.com/mp3s/theOtherTwo.mp3" length="22222" type="audio/mp4"/> <enclosure url="http://www.example3.com/mp3s/theOtherTwo.mp3" length="333" type="audio/mp3"/> <junk2 url="aaa" length="213123" /> '; $m = enclosureRead($s); echo '<pre>', print_r($m, true), '</pre>'; /** * Parse string * @param string $s The string that you wanr to parse * @return array of string */ function enclosureRead($s) { // Get matched enclosure(s) // Matched string is caught in $m_core[1] => array preg_match_all('/\<enclosure (.*)\>/Ui', $s, $m_core); $return = array(); // Loop for each matched enclosure(s) foreach ($m_core[1] as $core) { $return2 = array(); // Read attribute(s) and matched value(s) preg_match_all('/([a-z]+)\=\"(.*)\"/Ui', $core, $m); $i = 0; foreach ($m[1] as $m2) { $return2[$m[1][$i]] = $m[2][$i]; $i++; } $return[] = $return2; } return $return; } ?> PHP: And you will get this as output Array ( [0] => Array ( [url] => http://www.example.com/mp3s/theOtherOne.mp3 [length] => 6666097 [type] => audio/mpeg ) [1] => Array ( [url] => http://www.example2.com/mp3s/theOtherTwo.mp3 [length] => 22222 [type] => audio/mp4 ) [2] => Array ( [url] => http://www.example3.com/mp3s/theOtherTwo.mp3 [length] => 333 [type] => audio/mp3 ) ) Code (markup):