php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #38350 regexpr string causes error at runtime.
Submitted: 2006-08-05 18:09 UTC Modified: 2006-08-05 18:27 UTC
From: syacko at nayaco dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.1.4 OS: RHEL 4 update 3 Linux
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: syacko at nayaco dot com
New email:
PHP Version: OS:

 

 [2006-08-05 18:09 UTC] syacko at nayaco dot com
Description:
------------
In a class, using a private variable with a regular expression will cause a parseing error. The same regular expression in a method variable will not.

Error Message:
Parse error: syntax error, unexpected '"' in /nayaco/apps/releasemanager/test.php on line 8

Reproduce code:
---------------
Code that does not work:
<?php
$x = new regClass();
$x->main();
$x = null; 
exit;
class regClass
{
private $myFilter = "/^NC([a-zA-Z]+)\.php$/";
public function main()
{
echo $this->myFilter."\n";
echo preg_match($this->myFilter, "NCScott.php", $myMatches);
print_r($myMatches);
}
}
?>

Code that works:
<?php
$x = new regClass();
$x->main();
$x = null; 
exit;
class regClass
{
public function main()
{
$myFilter = "/^NC([a-zA-Z]+)\.php$/";
echo $myFilter."\n";
echo preg_match($myFilter, "NCScott.php", $myMatches);
print_r($myMatches);
}
}
?>

Expected result:
----------------
Both sets of code should produce the same result:
/^NC([a-zA-Z]+)\.php$/
1Array
(
    [0] => NCScott.php
    [1] => Scott
)


Actual result:
--------------
Parse error: syntax error, unexpected '"' in /nayaco/apps/releasemanager/test.php on line 8

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-08-05 18:19 UTC] tony2001@php.net
private $myFilter = "/^NC([a-zA-Z]+)\.php$/";
"$" in strings means start of the variable.

You have to quote it to make the engine to ignore it:
private $myFilter = "/^NC([a-zA-Z]+)\.php\$/";
 [2006-08-05 18:25 UTC] syacko at nayaco dot com
Ok, fair enough on the quoting. The question is why does the second example work. The bug is that there is different behavior for the same action. Either way the parsing engine should see the $ and generate an error.
 [2006-08-05 18:27 UTC] tony2001@php.net
Because variables/expressions are not allowed when initializing class attributes.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Thu Oct 08 03:00:02 2026 UTC