Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: preg_match; Not able to match the £ symbol

Tags:

regex

php

I've really been wracking my brains over this one, as for the life of me I can't figure out what the problem is.

I've got some data I want to run a regular expression on. For reference, the original document is encoded in iso-8859-15, if that makes any difference.

Here is a function using the regular expression;

if(preg_match("{£\d+\.\d+}", $handle)) // 
{
    echo 'Found a match';

}
else
{
    echo 'No match found';
}

No matter what I try I can't seem to get it to match. I've tried just searching for the £ symbol. I've gone over my regular expression and there aren't any issues there. I've even pasted the source data directly into a regular expression tester and it finds a complete match for what I'm looking for. I just don't understand why my regular expression isn't working. I've looked at the raw data in my string that I'm searching for and the £ symbol is there as clear as day.

I get the feeling that there's some encoded character there that I just can't see, but no matter how I output the data all I can see is the £ symbol, but for whatever reason it's not being recognised.

Any ideas? Is there an absolute method to viewing raw data in a string? I've tried var_dump and var_export, but I do get the feeling that something isn't quite right, as var_export does display the data in a different language. How can I see what's "really" there in my variable?

I've even saved the content to a txt file. The £ is there. There should be no reason why I shouldn't be able to find it with my regular expression. I just don't get it. If I create a string and paste in the exact bit of test my regular expression should pick up, it finds the match without any problems.

Truly baffling.

like image 606
Martyn Shutt Avatar asked Nov 25 '12 20:11

Martyn Shutt


2 Answers

You could always transform the letter:

$string = '£100.00';
if(preg_match("/\xa3/",$string)){
    echo 'match found';
}else{
    echo 'no matches';
}
like image 101
Samuel Cook Avatar answered Nov 03 '22 02:11

Samuel Cook


You can include any character in your regular expression if you know the hexadecimal value. I think the value is 0A3H, so try this:

 \xa3  // Updated with the correct hex value
like image 42
Felipe Alameda A Avatar answered Nov 03 '22 04:11

Felipe Alameda A