Any questions involving matching text strings to patterns - the pattern is called a "regular expression."
Moderator: General Moderators
-
nwp
- Forum Contributor
- Posts: 105
- Joined: Sun Feb 04, 2007 12:25 pm
Post
by nwp »
I am using this code
Code: Select all
<?php
$sub = 'My foo[0]';
$replace = 'bar[0]';
echo preg_replace('/foo[0]/', $replace, $sub);
?>
And its outputing
My foo[0]
instead of My bar[0].
Is it for the '[' , ']' ?
If yes how to do it ??
-
feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Post
by feyd »
The brackets are special characters in regular expressions. They must be escaped if you wish to have their literal character meaning.