Jump to content

Newbie Help needed - preg_match regex line break issues!


youngnickodemus

Recommended Posts

Hi all,

 

I'm really having issue with something that i think should be very basic, but it's really starting to get on my nerves...if anyone can help i'd really appreciate it!

 

Basically i've written a price scraping script which works fine for most things, but i'm struggling to write the regex for one particular price i'm trying to scrape from a site.

 

The html i'm searching through is:

 

<span class="lprice">

      £312.34

</span>

 

I want to extract the price '312.34', but i can't get any joy at all.

 

The php i'm using is basically:

 

$expression = "<span class=\"lprice\">\n£(.*?)\n<\/span>";

preg_match($webpage, $expression, $result);

 

I'd really appreciate any advice!

 

THanks,

 

Nick

 

 

You missed the space/tabs between the span open tag newline and the £

 

Thanks Mike.

 

I just tried:

 

<span class=\"lprice\">\n(.*?)£(.*?)\n<\/span>

 

and that didn't work either....? any ideas??

 

Cheers,

 

Nick

<?php
$string = '<span class="lprice">
       £312.34
</span>';

preg_match("~<span.+?;(.+?)</span>~is", $string, $matches);

$price = $matches[1];

echo $price;

die();
?>

 

Should do what you want.

 

As a note if you have multiple spans with a ; inside of them you may want to add the class= part. If you do not then this should be fine, especially if it is the first span, as this will only match the first item on the page that matches that regex.

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.