ID: 15166
Updated by: sander
Old Summary: preg_replace issue when \\1 is followed by 1
Reported By: [EMAIL PROTECTED]
Status: Open
Old Bug Type: *Regular Expressions
Bug Type: Documentation problem
Operating System: WINDOWS 2000
PHP Version: 4.1.1
New Comment:

\\11 matches the 11th string enclosed by parenthesis.
Can't find anything about escaping it. Reclassified as a documentation
problem.


Previous Comments:
------------------------------------------------------------------------

[2002-01-22 11:47:39] [EMAIL PROTECTED]

1/DETAIL:
When I am using preg_replace with \\1 to keep the first iteration
retrieved, if \\1 is followed by a string starting by 1, this is
removing the 1 of the string and losing the content of \\1.

Here is a script to show 4 test cases. The test2 and test4 failed
because of this issue. 

The test3 is the solution i found to avoid this issue by inserting any
character different from 1 just after \\1.

Thanks in advance for your help.
Pierre

<?
$value='<!DOCTYPE map SYSTEM
"http://localhost/Gui/dtd/deployment/dbmap.dtd";>';
$servname='10.0.9.91';
$servport='81';

// TITLE
print "<HR><CENTER>\n";
print("<b>BUG with <font color='red'>preg_replace</font> and <font
color='red'>\\\\1</font> followed by a variable starting by
1</b><BR><BR>\n");
print("<B> WINDOWS 2000 platform, PHP version 4.1.1<BR></b>\n");
print("<xmp>ORIGINAL VALUE:".$value."</xmp>\n");
print "<HR></CENTER>\n";

// TEST 1 with servname='localhost';
$testservname='localhost';
$test=$value;
$expected='<!DOCTYPE map SYSTEM
"http://'.$testservname.':'.$servport.'/Gui/dtd/deployment/dbmap.dtd">';
$test=preg_replace("/(<\!DOCTYPE\s+map\s+SYSTEM\s+\"http:\/\/)[^\/]+/i","\\1$testservname:$servport",$test);

$status ='OK';
if ($test != $expected) $status='WRONG';

print "<xmp>\n";
print_r("test1 SERVER NAME:$testservname        SERVER PORT:$servport\n");
print_r("test1 OPERATION:
\$test=preg_replace(\"/(<\!DOCTYPE\s+map\s+SYSTEM\s+\"http:\/\/)[^\/]+/i\",\"\\\\1\$testservname:\$servport\",\$test);"."\n");

print_r("test1 RESULT  :".$test."\n");
print_r("test1 EXPECTED:".$expected."\n");
print "</xmp><BR>\n";
print("test1 STATUS  :<font
color='red'>".$status."</font><BR><HR>\n");

//TEST 2 with servname = '10.0.9.91';
$test=$value;
$testservname=$servname;
$expected='<!DOCTYPE map SYSTEM
"http://'.$testservname.':'.$servport.'/Gui/dtd/deployment/dbmap.dtd">';
$test=preg_replace("/(<\!DOCTYPE\s+map\s+SYSTEM\s+\"http:\/\/)[^\/]+/i","\\1$servname:$servport",$test);

$status ='OK';
if ($test != $expected) $status='WRONG';

print "<xmp>\n";
print_r("test2 SERVER NAME:$testservname        SERVER PORT:$servport\n");
print_r("test2 OPERATION:
\$test=preg_replace(\"/(<\!DOCTYPE\s+map\s+SYSTEM\s+\"http:\/\/)[^\/]+/i\",\"\\\\1\$testservname:\$servport\",\$test);"."\n");

print_r("test2 RESULT  :".$test."\n");
print_r("test2 EXPECTED:".$expected."\n");
print"</xmp><BR>\n";
print("test2 STATUS  :<font
color='red'>".$status."</font><BR><HR>\n");


// TEST3
$test=$value;
$testservname=$servname;
$expected='<!DOCTYPE map SYSTEM
"http://'.$testservname.':'.$servport.'/Gui/dtd/deployment/dbmap.dtd">';
$test=preg_replace("/(<\!DOCTYPE\s+map\s+SYSTEM\s+\")http:\/\/[^\/]+/i","\\1http://$servname:$servport",$test);

$status ='OK';
if ($test != $expected) $status='WRONG';

print "<xmp>\n";
print_r("test3 SERVER NAME:$testservname        SERVER PORT:$servport\n");
print_r("test3 OPERATION:
\$test=preg_replace(\"/(<\!DOCTYPE\s+map\s+SYSTEM\s+\")http:\/\/[^\/]+/i\",\"\\\\1http://\$testservname:\$servport\",\$test);"."\n");

print_r("test3 RESULT  :".$test."\n");
print_r("test3 EXPECTED:".$expected."\n");
print "</xmp><BR>\n";
print("test3 STATUS  :<font
color='red'>".$status."</font><BR><HR>\n");


// TEST4
$test="this is a test with any string";
$testservname='1234567890';
$expected='this is a '.$testservname.':'.$servport.' with any string';
$test=preg_replace("/(this is a
)test/i","\\1$testservname:$servport",$test);
$status ='OK';
if ($test != $expected) $status='WRONG';

print "<xmp>\n";
print_r("test4 SERVER NAME:$testservname        SERVER PORT:$servport\n");
print_r("test4 OPERATION: \$test=preg_replace(\"/(this is
a)test/i\",\"\\\\1\$testservname:\$servport\",\$test);"."\n");
print_r("test4 RESULT  :".$test."\n");
print_r("test4 EXPECTED:".$expected."\n");
print "</xmp><BR>\n";
print("test4 STATUS  :<font
color='red'>".$status."</font><BR><HR>\n");

// General comments
print "<BR><b>COMMENTS</b>: test2 and test4 failed<BR>\n";
print "The issue happens only if \$testservname start by 1. For example
with test1 the substitution works since the value of \$testservname
does not start by 1\n";
print "<HR>\n";
?>

------------------------------------------------------------------------



Edit this bug report at http://bugs.php.net/?id=15166&edit=1


-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to