Jump to content

Running preg on DOMDocument node?


emirpprime

Recommended Posts

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  :D

 

Phil

Link to comment
https://forums.phpfreaks.com/topic/216879-running-preg-on-domdocument-node/
Share on other sites

$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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.