|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Rik Wasmus wrote:
> > You are most likely looking for \b (word boundary) Thanks Rik for all your . 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. |
|
![]() |
| Outils de la discussion | |
|
|