regular expression to underline a given word in a text… – PHP

  php

Q(Question):

Hi,

With the sentence :

"Bordeaux est au bord de l’eau"

How to do to underline, for instance, the word "eau" ? without underlining
the substring of "Bordeaux" ?
I don’t know how to isolate the word…

My current code :

$text=eregi_replace("(".stripslashes($word_to_unde rline]).")","<b>\\0</b>",$
text);

but this underline "eau" in "Bordeaux" too and i don’t want to !

Thanks for any help !

Fred

A(Answer):

I <bf**********@news-reader2.wanadoo.fr>, Fred skrev:

How to do to underline, for instance, the word "eau" ? without underlining
the substring of "Bordeaux" ?
I don’t know how to isolate the word…

Try:

preg_replace("/[^\w](eau)[^\w]/", "<u>\\1</u>", $text);
My current code :
$text=eregi_replace("(".stripslashes($word_to_unde rline]).")","<b>\\0</b>",$text);

Besides, use <u> for underlining text. <b> is for bold text.

mvrgr, Wouter


:wq mail uw*@xs4all.nl

de welvaartstaat? :: of die bestaat? :: ze vraagt zich echt af — monza

A(Answer):

Re,

Thanks !
it works perfectly !
Some improvement :

preg_replace("/([^\w])(".stripslashes($tab_mots[$i]).")([^\w])/i",
"\\1<b>\\2</b>\\3", $texte);

(to correct a bug searching "l’eau" for instance)

Now, how to find the position of this word in a text ?
(not "eau" in "bordeaux" but the whole word "eau")

Regards,

fred

"stephan beal" <st*****@wanderinghorse.net> a écrit dans le message de
news:bf**********@ork.noris.net…

Fred wrote:

How to do to be case insensitive ?

preg_replace("/[^\w](eau)[^\w]/i", "<u>\\1</u>", $text);

note the ‘i’ after the last / of the pattern.


—– stephan beal
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.

A(Answer):

In article <bf**********@news-reader2.wanadoo.fr>,
"Fred" <fr******@free.fr> wrote:

"Bordeaux est au bord de l’eau"

How to do to underline, for instance, the word "eau" ? without underlining
the substring of "Bordeaux" ?
I don’t know how to isolate the word…

My current code :

$text=eregi_replace("(".stripslashes($word_to_unde rline]).")","<b>\\0</b>",$
text);

but this underline "eau" in "Bordeaux" too and i don’t want to !

$unbolded=stripslashes($text);
$bolded=preg_replace("/\b(eau)\b/", "<b>$1</b>", $unbolded);

Note that matching will be case-sensitive.


CC

A(Answer):

I <bf**********@news-reader5.wanadoo.fr>, Fred skrev:

Now, how to find the position of this word in a text ?
(not "eau" in "bordeaux" but the whole word "eau")

Look at strpos()

mvrgr, Wouter


:wq mail uw*@xs4all.nl

and it’s you i see :: but you don’t see me — coldplay

A(Answer):

In article <bf**********@ork.noris.net>,
stephan beal <st*****@wanderinghorse.net> wrote:

Fred wrote:

How to do to be case insensitive ?

preg_replace("/[^\w](eau)[^\w]/i", "<u>\\1</u>", $text);

note the ‘i’ after the last / of the pattern.

Note that doing so matches things like "eAu", "EaU", etc. but *only where
some character not in the set [a-zA-Z0-0_] occurs both before and after*.
If you also want to match against something like "Eau d’whatever", you need
a better regex. Check out the solution I already offered using word
borders \b. And if you want only the first letter to be case insensitive,
substitute "/\b([eE]au)\b/" instead.


CC

A(Answer):

"Fred" <fr******@free.fr> wrote in
news:bf**********@news-reader2.wanadoo.fr:

With the sentence :

"Bordeaux est au bord de l’eau"
<<snipped>>

Please do not multipost. If you feel the need for getting your post in
multiple newsgroups, crosspost (add additional newsgroup names to the
newsgroups: header in a comma-separated list). It saves the same solutions
being repeated over and over in different groups, thus wasting the time of
the people responding (as happened with the time I spent replying to you).

KAH

LEAVE A COMMENT