What about something like:
<?php
function makeBold($var){
return (ereg_replace("[a|b]+[a-z]+[a-z]","<b>\\0</b>",$var));
}
$row = "You are the best";
echo makeBold($row);
?>
Outputs: You are the best
buddylee17
Practically a Master Poster
697 posts since Nov 2007
Reputation Points: 232
Solved Threads: 137
Try this one out:
<?php
$group =array("A","a","B","b");
$row = "You are the best At BaseBall";
$all = implode("|",$group); // convert the array into a pipe delimited string
echo (ereg_replace($all,"<b>\\0</b>",$row));
?>
buddylee17
Practically a Master Poster
697 posts since Nov 2007
Reputation Points: 232
Solved Threads: 137
It would be a lot more efficient to just use the string replacement functions such as str_replace() or str_ireplace()
$replace = array('a', 'b');
$replacements = array('<b>a</b>', '<b>b</b>');
$subject = "You are the best At BaseBall";
$subject= str_ireplace($replace, $replacements, $subject);
echo $subject;
or
$replace = array('a', 'b');
$subject = "You are the best At BaseBall";
foreach($replace as $x) {
$subject = str_ireplace($x, '<b>'.$x.'</b>', $subject);
}
echo $subject;
The string functions are much faster then the regular expression engine.
If you do use regex, the PCRE functions are faster. http://www.php.net/preg_replace
digital-ether
Nearly a Posting Virtuoso
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101