Jump to content

Problem Matching British Pound Sign


shaunie

Recommended Posts

Hi,

 

Am having problems matching a british pound sign when scraping a page. The following code works fine:

 

<?php

   $url = 'http://www.mydomain.com';
   
   $html = file_get_contents($url);
   
   preg_match_all('/<span class="price">(.*?)<\/span>/', $html, $prices);
   
   foreach($prices[1] as $key => $price) {
            $result[$key]['price'] = $price;
        }
   
   print_r($result);

?>

 

However I don't want the pound sign included in the array array value. If I change the pattern to this:

 

preg_match_all('/<span class="price">£(.*?)<\/span>/', $html, $prices);

 

I get the following error:

 

Notice: Undefined variable: result in C:\wamp\www\PDK Hub\web\cron_scripts\scrape4.php on line 24

 

I thought the document may have the pound sign encoded with the HTML name or sign but it doesn't...

 

Any ideas how I can match this?

Link to comment
Share on other sites

I suspect you will have the same problem with any character outside the "common" character set. When dealing with ASCII vs UTF, vs etc. You can get some odd results. Instead of trying to match the pound symbol, you can take another approach.

 

Before I give a couple of possible solutions, the current pattern is inefficient. Instead of using ".*?", I would asume the data withint he SPAN tags does not have a "<". In tat case you should change the pattern to find all chaacters NOT a "<".

 

Anyway, If the value inside the SPAN tag always has a pound symbol, then just create the pattern to not include the first character.

preg_match_all('/<span class="price">.([^<]*)<\/span>/', $html, $prices);

 

Another approach is to find all characters between the span tags that are numerals or the decimal point. But, I'd give that first one a try.

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.