RE: marking words bold

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> Include a space in your str_replace statement.
> 
> For instance
> 
> $t = str_replace(" $word ", "<B> $word </B>", $text);
> 
> That should prevent the problem your having and ensure only individual
> words
> are bolded.

Not the best solution if Merlin's code needs to account for the possibility
of target words being preceded or followed by punctuation.

Consider wanting to bold the word "tour" in the following sentence:

$text = "At this point we will commence our tour, guided by an operator
recommended by the department of tourism.";

$t = str_replace(" tour ", "<b>tour</b>", $text);

... would fail, because the word "tour" in the sentence is immediately
followed by a comma.

$t = preg_replace("/\btour\b/", "<b>tour</b>", $text);

... would work, because the comma forms a word boundary, with the added
benefit that the "tour" in "tourism" would also remain untouched because
there is word text directly after the "r" in "tourism".

Note: I realize your solution fits the example Merlin gave, but preg_replace
offers a great deal more flexibility if you are not aware, ahead of time,
exactly what the target string will contain.

Just thought it was worth mentioning.

Much warmth,

Murray

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux