Jump to content

[SOLVED] Regex; Getting similar entries multiple times?


Recommended Posts

Okay, I'm using Regex to pull the names of certain power tools from an xml style home improvement blog.

 

Basically, this page is made up of

 

<item>
<name>Name</name>
<description>Description</description>
<Format id="xhr_3452" />
</item>

 

Over and over again.

 

Now, I'm trying to get the contents of Format id, which in this case is xhr_3452.

 

 

$fgc = file_get_contents('http://www.site.org/feed/btg.xml');
preg_match('{id="xhr_([0-9]+)" /}', $fgc , $cms );
echo $cms[1];

 

And using the code above, I can do that just fine, however theres about 20 other entries on that page in the same format using format id="", but with different content.

 

How can I use regex to get not only the first instance of format id="", but also all OTHERS on the page.

 

So if we see

 

<item>
<name>Name</name>
<description>Description</description>
<Format id="xhr_3452" />
</item>

<item>
<name>Name</name>
<description>Description</description>
<Format id="xhr_4564 />
</item>

<item>
<name>Name</name>
<description>Description</description>
<Format id="xhr_524" />
</item>

<item>
<name>Name</name>
<description>Description</description>
<Format id="xhr_8423" />
</item>

 

I can get ALL the format id's. Also note, the 'xhr_' will not change, so basically all that needs to be retrieved are the numbers. :)

try this

<?php
$fgc = file_get_contents('http://www.site.org/feed/btg.xml');

preg_match_all('/id="xhr_([0-9]+)"/si', $fgc, $cms, PREG_PATTERN_ORDER);
$result = $cms[1];
echo "<pre>";print_r($result);echo "</pre>";
?>

 

Yeah, exactly what I needed. Thanks. :)

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.