emirpprime Posted October 26, 2010 Share Posted October 26, 2010 Hi, I have a RSS feed cached as an XML file. I need to pull some info out of it so I can then print it to the page. Currently I am using this code to extract the data: $doc = new DOMDocument(); $doc->load('inthenews.xml'); $inthenews = array(); foreach ($doc->getElementsByTagName('item') as $node) { $itemRSS = array ( 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, 'link' => $node->getElementsByTagName('link')->item(0)->nodeValue, 'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue, ); array_push($inthenews, $itemRSS); } However, the Description node contains more than I want. I need to remove everything except for the image ( <img../> ) it contains. Is there some way of running preg or similar on the nodeValue as it is extracted? Or an alternative to "getElementsByTagName" that allows searching for strings? If not, does anyone have a suggestion for doing this? I tried running preg_replace on the array, but it doesn't seem to do anything?? An example of the Array created by my code above is shown below: [0] => Array ( [title] => BBC radio Cambridge 7.20am [link] => images/news/Matthew_Freeman_Radio_Cambridgeshire_21-10-10.mp3 [desc] => <img alt="BBC-logo" src="http://www2.mrc-lmb.cam.ac.uk/images/news/BBC-logo.jpg" height="54" width="127" /><br/>BBC radio Cambridge 7.20am 21.10.10: Dr Matthew Freeman"<br/> 21 October 2010 ) Thanks in advance for any advice Phil Quote Link to comment https://forums.phpfreaks.com/topic/216879-running-preg-on-domdocument-node/ Share on other sites More sharing options...
sasa Posted October 26, 2010 Share Posted October 26, 2010 $doc = new DOMDocument(); $doc->load('inthenews.xml'); $inthenews = array(); foreach ($doc->getElementsByTagName('item') as $node) { preg_match('/<img\s[^>]*/i', $node->getElementsByTagName('description')->item(0)->nodeValue, $out) $itemRSS = array ( 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, 'link' => $node->getElementsByTagName('link')->item(0)->nodeValue, 'desc' => $out ); array_push($inthenews, $itemRSS); } not tested Quote Link to comment https://forums.phpfreaks.com/topic/216879-running-preg-on-domdocument-node/#findComment-1126743 Share on other sites More sharing options...
emirpprime Posted October 28, 2010 Author Share Posted October 28, 2010 Thanks a lot sasa! I'll test it out when I get back from this conference on Monday. All the best, Phil Quote Link to comment https://forums.phpfreaks.com/topic/216879-running-preg-on-domdocument-node/#findComment-1127453 Share on other sites More sharing options...
sasa Posted October 29, 2010 Share Posted October 29, 2010 ups i make some mistake $doc = new DOMDocument(); $doc->load('inthenews.xml'); $inthenews = array(); foreach ($doc->getElementsByTagName('item') as $node) { preg_match('/<img\s[^>]*>/i', $node->getElementsByTagName('description')->item(0)->nodeValue, $out); // $itemRSS = array ( 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, 'link' => $node->getElementsByTagName('link')->item(0)->nodeValue, 'desc' => $out[0] ); array_push($inthenews, $itemRSS); } Quote Link to comment https://forums.phpfreaks.com/topic/216879-running-preg-on-domdocument-node/#findComment-1127916 Share on other sites More sharing options...
emirpprime Posted November 2, 2010 Author Share Posted November 2, 2010 Works perfectly! Thanks very much Phil Quote Link to comment https://forums.phpfreaks.com/topic/216879-running-preg-on-domdocument-node/#findComment-1129468 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.