By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,167 Members | 1,692 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,167 IT Pros & Developers. It's quick & easy.

ereg_replace problem

P: n/a
I have a string that has been saved in a database from a textarea form
field.

e.g.

$text = "Dear %name

The date is %date

yours,

%user"

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 ...".

Any ideas?
Jun 27 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hugh Oxford wrote:
I have a string that has been saved in a database from a textarea form
field.

e.g.

$text = "Dear %name

The date is %date

yours,

%user"

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 ...".
What do _you_ think '^' means?
Also, don't use ereg_*, switch to preg_*

$string = preg_replace('/%([a-z]+)/sie','[you asked for $1]',$string);
--
Rik Wasmus
....moving deadlines for your pleasure.
Jun 27 '08 #2

P: n/a
Rik Wasmus wrote:
Hugh Oxford wrote:
>I have a string that has been saved in a database from a textarea form
field.

e.g.

$text = "Dear %name

The date is %date

yours,

%user"

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 ...".

What do _you_ think '^' means?
Also, don't use ereg_*, switch to preg_*

$string = preg_replace('/%([a-z]+)/sie','[you asked for $1]',$string);
Erm, the /e doesn't belong there ('/%([a-z]+)/si'), I leave it to you to
define a desired callback (possibly with preg_replace_callback) to the
variable.
--
Rik Wasmus
....moving deadlines for your pleasure.
Jun 27 '08 #3

P: n/a
Rik Wasmus wrote:
Rik Wasmus wrote:
>Hugh Oxford wrote:
>>I have a string that has been saved in a database from a textarea
form field.

e.g.

$text = "Dear %name

The date is %date

yours,

%user"

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 ...".

What do _you_ think '^' means?
Also, don't use ereg_*, switch to preg_*

$string = preg_replace('/%([a-z]+)/sie','[you asked for $1]',$string);

Erm, the /e doesn't belong there ('/%([a-z]+)/si'), I leave it to you to
define a desired callback (possibly with preg_replace_callback) to the
variable.
Thanks Rik.

From what I've understood ^ means match the beginning of the string. If
I do ereg_replace('^%', 'foo', $text); it doesn't find anything at all.
Jun 27 '08 #4

P: n/a
Hugh Oxford wrote:
Rik Wasmus wrote:
>Rik Wasmus wrote:
>>Hugh Oxford wrote:
I have a string that has been saved in a database from a textarea
form field.

e.g.

$text = "Dear %name

The date is %date

yours,

%user"

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 ...".

What do _you_ think '^' means?
Also, don't use ereg_*, switch to preg_*

$string = preg_replace('/%([a-z]+)/sie','[you asked for $1]',$string);

Erm, the /e doesn't belong there ('/%([a-z]+)/si'), I leave it to you
to define a desired callback (possibly with preg_replace_callback) to
the variable.

Thanks Rik.

From what I've understood ^ means match the beginning of the string. If
I do ereg_replace('^%', 'foo', $text); it doesn't find anything at all.
^ 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)
--
Rik Wasmus
....moving deadlines for your pleasure.
Jun 27 '08 #5

P: n/a
Rik Wasmus wrote:
>
You are most likely looking for \b (word boundary)
Thanks Rik for all your help. It is appreciated. I used your code and
wrote a callback function.
FFR, to use an object method as a callback function you need to use
array($this, 'function_name') as the callback function.
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.