php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #32068 Parse not executed while using the perl comment(#) with patterns
Submitted: 2005-02-22 22:56 UTC Modified: 2005-02-22 23:12 UTC
From: simon at ruderich dot com Assigned:
Status: Not a bug Package: Output Control
PHP Version: 4.3.10 OS: Mac OS X 10.3.8
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: simon at ruderich dot com
New email:
PHP Version: OS:

 

 [2005-02-22 22:56 UTC] simon at ruderich dot com
Description:
------------
I wanted to replace a string using the 'preg_replace'-
function. I tried more patterns, so I commented out the 
old patterns. But then it showed me not the replace 
string. This must be a problem with the perl comment (#) 
because if I use /* */ to comment out the old pattern 
the script works.

Reproduce code:
---------------
<?php
  error_reporting(E_ALL);

  $string = " <a> asdf </a>  <a>   <a>     test   </a>   <a>     test   </a>   <a>     test   </a> </a>  <a>asdf</a>  <a>asdf</a> ";

  #$pattern = '#<a>( ( ([^<>])|(?R) )*? )</a>#isx';
  #$pattern = '#<a>( ( (?R)|() )*? )</a>#isx';
  #$pattern = '#\( (( (?>[^()]+) | (?R) )*) \)#isUx';

  $pattern = '#<a>( (.)*? )</a>#isxU';
  $replace = '[a]$1[/a]';

  echo '<pre>'."\n";
  echo htmlspecialchars($string);
  echo "\n\n";
  echo htmlspecialchars(preg_replace($pattern, $replace, $string));
  echo '</pre>'."\n";

 ?>

Expected result:
----------------
 <a> asdf </a>  <a>   <a>     test   </a>   <a>     test   
</a>   <a>     test   </a> </a>  <a>asdf</a>  <a>asdf</
a> 

 [a] asdf </a>  <a>   <a>     test   </a>   <a>     test   
</a>   <a>     test   </a> </a>  <a>asdf</a>  <a>asdf[/
a] 

Actual result:
--------------
[^()]+) | (?R) )*) \)#isUx';  $pattern = '#( (.)*? 
)#isxU';  $replace = '[a]$1[/a]';  echo '
'."\n";
  echo htmlspecialchars($string);
  echo "\n\n";
  echo htmlspecialchars(preg_replace($pattern, $replace, 
$string));
  echo '
'."\n";  ?>

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-02-22 23:12 UTC] derick@php.net
This line:
  #$pattern = '#\( (( (?>[^()]+) | (?R) )*) \)#isUx';

has a ?> in it, which marks the end of a script block.
No bug here (this is also documented in the manual)

 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 13:01:33 2025 UTC