Discussion: ereg_replace problem
Afficher un message
Vieux 20/06/2008, 11h34   #4
Hugh Oxford
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ereg_replace problem

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.
  Réponse avec citation
 
Page generated in 0,04633 seconds with 9 queries