954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Trying to BOLD characters in string w/ preg_replace from multi-dimensional array???

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

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

It also does the same for

shaneog
Newbie Poster
10 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

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 ab>re the best"

<?php

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

?>
shaneog
Newbie Poster
10 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

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
 

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???

shaneog
Newbie Poster
10 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

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
 

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

shaneog
Newbie Poster
10 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

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

Thanks

shaneog
Newbie Poster
10 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

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
Moderator
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You