php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64145 preg_match return false for using (\$)
Submitted: 2013-02-04 19:30 UTC Modified: 2013-02-04 19:50 UTC
From: mrsnikivan at gmail dot com Assigned:
Status: Not a bug Package: PCRE related
PHP Version: 5.3.21 OS: window
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: mrsnikivan at gmail dot com
New email:
PHP Version: OS:

 

 [2013-02-04 19:30 UTC] mrsnikivan at gmail dot com
Description:
------------
I using worksheet de pear(library parse.php, found problem for preg_match)

$token='=sum($a1:$a1);
if (preg_match("/^(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+:(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+$/",$token)) {
...
}
no true
$token='=sum(a1:a1);
if (preg_match("/^(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+:(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+$/",$token)) {
...
}
is true

Test script:
---------------
$ change another different character and no problem sample @

$token='=sum(@a1:@a1);
if (preg_match("/^(\@)?[A-Ia-i]?[A-Za-z](\@)?[0-9]+:(\@)?[A-Ia-i]?[A-Za-z](\@)?[0-9]+$/",$token)) {
...
}

After trying other caracters I found that the problem is that the $ replace internal a point (.) character 46

$token='=sum($a1:$a1);
if (preg_match("/^(.)?[A-Ia-i]?[A-Za-z](.)?[0-9]+:(.)?[A-Ia-i]?[A-Za-z](.)?[0-9]+$/",$token)) {
...
}
here if there was affirmative result for dollar ($) and point (.)



Expected result:
----------------
true for (\$) for preg_match

Actual result:
--------------
false for (\$) for preg_match

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-02-04 19:50 UTC] krakjoe@php.net
-Status: Open +Status: Not a bug
 [2013-02-04 19:50 UTC] krakjoe@php.net
The pattern you gave is never going to match the token provided, [A-Ia-i] will 
never match sum; I assume the token has been provided incorrect:

<?php
$token='$a1:$a1';
var_dump(preg_match("/^(\\$)?[A-Ia-i]?[A-Za-z](\\$)?[0-9]+:(\\$)?[A-Ia-i]?[A-Za-
z](\\$)?[0-9]+$/",$token, $matches));
print_r($matches);
?>

You need to escape $ twice when using double quotes.

<?php
$token='$a1:$a1';
var_dump(preg_match('/^(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+:(\$)?[A-Ia-i]?[A-Za-z]
(\$)?[0-9]+$/',$token, $matches));
print_r($matches);
?>

Both examples function as expected.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 13 04:01:34 2025 UTC