RE: marking words bold

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

 



> I am trying to mark words inside a sentence bold. Problem is, if there is
> an
> overlap it does not work anymore.
> I am using this code:  $t = str_replace($word, "<b>$word</b>", $text);
> 
> For eample:
> Mark those words bold: adventure in singapore
> Text: My adventure flying to singapore
> 
> The problem lays in the word "in". The code I use does produce following:
> <b>s<b>in</b>gapore</b>
> which of course does not work properly.
> 
> Does anybody have a good sugestion on how to improve this?`
> 

Hi Merlin,

Sounds like you need to use preg_replace, specifically testing for word
boundaries. See http://au2.php.net/preg_replace and
http://php.mirrors.ilisys.com.au/manual/en/reference.pcre.pattern.syntax.php
for the \b word boundary syntax.

Not tested, but something like the following would probably be along the
right lines:

$thing = preg_replace('/\bin\b/i','<b>in</b>','adventures in singapore');
echo $thing;

produces: "adventures <b>in</b> singapore", ignoring the 'in' characters
within 'singapore' because they don't form a word boundary

Hope this helps,

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