Jump to content

[SOLVED] Regex; Getting similar entries multiple times?


Dale_G

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. :)

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.