Page 1 of 1
preg_replace
Posted: Sat Jun 05, 2004 6:44 pm
by Steveo31
I'm confused on the second parameter of preg_replace. All the examples I have seen have numbers and whatnot in em.
Could someone give me an example? I've read the manual

Posted: Sat Jun 05, 2004 8:41 pm
by markl999
Simple example that replaces 'fox' with 'new fox'
Code: Select all
<?php
$string = "The quick brown fox jumped over the lazy dog.";
echo preg_replace('/fox/', 'new fox', $string);
//or do the following
//$string = preg_replace('/fox/', 'new fox', $string);
//echo $string;
?>
Posted: Sat Jun 05, 2004 8:49 pm
by Steveo31
Huh.. simpler than I thought.
So then what would be all this garbled stuff?
Code: Select all
function MakeUrl($strUrl)
{
$strUrl = preg_replace("/(http(s)?:\\/\\/[^\\s\\n]*)\\b(\\/)?/i","<a href="\\\\0">\\\\0</a>",$strUrl);
return $strUrl;
}
I understand regex in terms of matching, but escaping \ and adding numbers is throwing me waaaay off base.
Thanks mark
Posted: Sat Jun 05, 2004 8:57 pm
by markl999
You can't really explain regex on a forum, have to google for 'regex tutorial' ... it's a massive subject.
Posted: Sat Jun 05, 2004 9:04 pm
by tim
Posted: Sat Jun 05, 2004 9:15 pm
by Steveo31
Afraid of that. Thanks mark and tim for the link
