Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP REGEX My 2 cents doesn't work! 1

Status
Not open for further replies.

sen5241b

IS-IT--Management
Sep 27, 2007
199
US
The first half of the code matches the word Look successfully. The second half matches the word Look with one copyright sign in place of an 'o'. The REGEX will not match using the copyright sign! Why doesn't this work?!!

In addition to the copyright sign, I've tried the cents sign and it also will not match. I've copy-pasted the REGEXs and strings below into Regex Coach (a REGEX tester) and they do match so the REGEXs are syntactically fine. I'm using PCRE 7.3 and PHP 5.2.5.


Code:
<?PHP
$iOffset = 0;
echo '<br> try simple regex';
$usethis = '/([L][o][o\xA2\xA9\x80][k])/ixu'; // Hex for cents, copyright and euro signs
$Str = 'Look up my friend';
echo '<br> $str=';
var_dump($Str);
$rtcode = preg_match_all($usethis, $Str, $TempArray, PREG_OFFSET_CAPTURE, $iOffset);  
echo ' <br> $rtcode=' . $rtcode;
if (!$rtcode) echo '<br> NO  match';
if ($rtcode)
{                            //preg_match returns $TempArray
	echo '<br> simple regex matched! $TempArray=';
	var_dump($TempArray[0]);
}

echo '<br> try matching copyright sign -hex A9';
$usethis = '/([L][o][o\xA2\xA9\x80][k])/ixu';
$Str = 'Lo©k up my friend';
echo '<br> $str=';
var_dump($Str); 
$rtcode = preg_match_all($usethis, $Str, $TempArray, PREG_OFFSET_CAPTURE, $iOffset);  
echo ' <br> $rtcode=' . $rtcode;
if (!$rtcode) echo '<br> NO  match';
if ($rtcode)
{
	echo '<br> simple something matched! $TempArray='; 
	var_dump($TempArray[0]);
}
?>

Here's the output:

Code:
try simple regex
$str=string(17) "Look up my friend"
$rtcode=1
simple regex matched! $TempArray=array(1) { [0]=> array(2) { [0]=> string(4) "Look" [1]=> int(0) } }
try matching copyright sign -hex A9
$str=string(17) "Lo©k up my friend"
$rtcode=0
NO match
 
try running the text through htmlspecialchars and then perform the regex using the html codes for the copyright sign etc.
 
Thanks for tip but htmlspecialchars did not help. I'm thinking maybe reporting a php bug. I assume its not the PCRE because the regexs work fine in REGEX coach.
 
It was the lowercase 'u' modifier.
 
thanks for posting back. i hadn't noticed the utf-8 modifier.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top