Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- ereg_replace problem
- Hugh Oxford
June 20, 2008, 8:56 am
rate this thread
I have a string that has been saved in a database from a textarea form
$text = "Dear %name
The date is %date
I wish to parse the string replacing every word beginning with % with a
variable $name, $date etc.
However, whenever I use ereg_replace, it just finds the beginning of the
whole string, rather than individual words.. eg
ereg_replace('^', 'foo', $text);
returns "fooDear %name ...".
Re: ereg_replace problem
Hugh Oxford wrote:
^ indeed matches the beginning of the string, meaning the string you
provide, not 'beginning of a seperate word'. A $string=
preg_replace('/^/','foo',$string); is the same as saying: $string =
'foo'.$string;. (You can alter this behaviour to make it also valid for
start of a line) An '/^%/' would only be found if you provided $string's
first character is '%'.
You are most likely looking for \b (word boundary)
...moving deadlines for your pleasure.