PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > ereg_replace problem
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
ereg_replace problem

Réponse
 
LinkBack Outils de la discussion
Vieux 20/06/2008, 09h56   #1
Hugh Oxford
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut ereg_replace problem

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?
  Réponse avec citation
Vieux 20/06/2008, 10h08   #2
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ereg_replace problem

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.
  Réponse avec citation
Vieux 20/06/2008, 10h10   #3
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ereg_replace problem

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.
  Réponse avec citation
Vieux 20/06/2008, 10h34   #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
Vieux 20/06/2008, 10h40   #5
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ereg_replace problem

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.
  Réponse avec citation
Vieux 20/06/2008, 12h06   #6
Hugh Oxford
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ereg_replace problem

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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 14h04.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16717 seconds with 14 queries