You are here: Re: marking words bold « PHP « IT news, forums, messages
Re: marking words bold

Posted by Ac on 05/12/05 00:16

Merlin wrote:
> 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

I whipped this together, it should work ok. You'll want to clean it up,
but you get the gist.

<?php
/* AC Was here */

$str="I'm going to the store to buy some stuff.";
$bold=array("store","some","stuff");


function boldWord($str,$bold) {
if(isset($str)) {
foreach($bold as $b) {
echo "$b should be <b>$b</b>\n";
$str = eregi_replace($b,"<b>$b</b>",$str);
}
$string=$str;
return $string;
} else {
$string='The inputed variable $str was empty.';
return $string;
}
} // End function bracket




// Example usage
if(isset($str)) {
$string=boldWord($str,$bold);
echo "Original: $str \n";
echo "New: $string\n";
} else {
echo "Variable str not set.";
}
?>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация