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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.