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

 

 

Link to comment
Share on other sites

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

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.