I have been racking my brain and can't figure this out???

I'm trying to <b> or <highlight> characters/words in my string and for some reason the <b> tag is getting replaced when the $q includes the character "b".

It also does the same for <span class=".... just reacts to different characters.

If anyone can help would be really great... or if you have better function???


$rows[0][0] = "The world is ending";
$rows[0][1] = "What should we do";
$rows[0][2] = "You are the best";
$rows[0][3] = "Turn left at the next exit";

$rows[1][0] = "You gave me life";
$rows[1][1] = "Will you be my friend";
$rows[1][2] = "I like you more everyday";
$rows[1][3] = "Is that a wig on your head";

$q = "a b c d";
$q_array = explode(" ", $q);
$q_array = array_unique($q_array);
$q_array = array_values($q_array);

$num_q = count($q_array);

foreach ($q_array as $key) $q_array1[] = "!($key)!i";

foreach($rows as $key){
  $row .= "<tr>";
  foreach($key as $str){
  $str = preg_replace($q_array1, "<b>$1</b>", $str);
  $row .= "<td>$str</td>";}
$row .= "</tr>";

echo "<table border=\"1\">$row</table>";



Recommended Answers

All 7 Replies

I'm trying to get this to work 100% of the time no matter what order the "b" character is in the string / array. This is because $q will be a search query. Any ideas?????

Here is another broken down example... it outputs:

"You <b>ab>re the best"


$row = "You are the best";

$q = "a b";

$q_array = explode(" ", $q);

foreach ($q_array as $key) $q_array1[] = "!($key)!i";

$str = preg_replace($q_array1, "<b>$1</b>", $row);

echo $str; 


What about something like:

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 <b>are</b> the <b>best</b>

Thanks for the function! However, I need it to BOLD the characters only, not the entire word. And it also needs to be case insensitive and work with arrays... any more ideas???

Try this one out:

$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));

Thanks for your help buddylee17! Got it to work case insensitive using eregi_replace.

Just wondering if you might know... is there an easier way to replace characters in an array without converting the array into strings?

For example, if I want to replace characters in an array I need to use:

foreach($row as $str) echo(eregi_replace($q,"<b>\\0</b>",$str));

So rather than running the function over an over it could just replace characters inside the array? Sort of like preg_replace.

Let me know if you think there is a way...


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;


$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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.