shaunie Posted February 20, 2011 Share Posted February 20, 2011 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? Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 21, 2011 Share Posted February 21, 2011 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.