For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > October 2006 > preg_match works, preg_replace does not - why?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author preg_match works, preg_replace does not - why?
bwooster47@gmail.com

2006-10-30, 7:03 pm

Using PHP 5.1.4 (cli) (built: May 8 2006 08:41:41)

The following script seems to show a problem with preg_replace - trying
to replace zero or more ending / characters, with a single A character.

preg_match shows that it matched the two // characters, but
preg_replace, with same pattern, seems to match no characters. Any
help?

<?php
$s = "aa//";

preg_match('/\/*$/', $s, $a);

echo "matches :" . $a[0] . ":\n"; // output: matches ://:

preg_replace('/\/*$/', 'A', $s);

echo "after replace :$s:\n"; // output: after replace :aa//:
// should be aaA and not above
?>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com