|
Posted by tg-php on 05/11/05 22:04
That's a good first step, but I think you're going to have to go with the regex for this one. What happens if one of the words he wants to highlight is near punctuation?
> $t = str_replace(" $word ", "<B> $word </B>", $text);
This wouldn't work if you had:
$text = "I'm going to the store.";
$word = "store";
Padding with spaces is good thinking.. just need to take it that extra step further.
-TG
= = = Original message = = =
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.
<?php
/*
Stephen Johnson c | eh
The Lone Coder
http://www.thelonecoder.com
stephen@thelonecoder.com
562.924.4454 (office)
562.924.4075 (fax)
continuing the struggle against bad code
*/
?>
> From: Merlin <news.groups@web.de>
> Date: Wed, 11 May 2005 17:34:56 +0200
> To: <php-general@lists.php.net>
> Subject: [PHP] marking words bold
>
> Hi there,
>
> 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?`
>
> Thank you for any help,
>
> merlin
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.
[Back to original message]
|