473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

regular expression to underline a given word in a text...

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
Jul 16 '05 #1
6 9247
uws
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
Jul 16 '05 #2
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.

Jul 16 '05 #3
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
Jul 16 '05 #4
uws
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
Jul 16 '05 #5
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
Jul 16 '05 #6
KAH
"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
Jul 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
4
by: Buddy | last post by:
Can someone please show me how to create a regular expression to do the following My text is set to MyColumn{1, 100} Test I want a regular expression that sets the text to the following...
2
by: Dan Schumm | last post by:
I'm relatively new to regular expressions and was looking for some help on a problem that I need to solve. Basically, given an HTML string, I need to highlight certain words within the text of the...
9
by: Schorschi | last post by:
Not having used regular expressions much, I need some help. Given a string... "This\0Guy\0Needs\0Some\0Help\0\0\0\0\0" Need result as array of strings... "This","Guy", "Needs", "Some", "Help" ...
0
by: leeonions | last post by:
Hi there, i am trying to use regular expressions to search through a text string and replace a given whole word. take the string = "The matsat on the mat!" (bad example i know) i want to...
2
by: leeonions | last post by:
Hi there, i am trying to use regular expressions to search through a text string and replace a given whole word. take the string = "The matsat on the mat!" (bad example i know) i want to...
6
by: Ludwig | last post by:
Hi, i'm using the regular expression \b\w to find the beginning of a word, in my C# application. If the word is 'public', for example, it works. However, if the word is '<public', it does not...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
0
by: altavim | last post by:
Usually when you make regular expression to extract text you are starting from simple expression. When you got to know target text, you are extending your expression. Subsequently very hard to ready...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.