943,733 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 2470
  • PHP RSS
Jan 10th, 2009
0

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

Expand Post »
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???

PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. $rows[0][0] = "The world is ending";
  4. $rows[0][1] = "What should we do";
  5. $rows[0][2] = "You are the best";
  6. $rows[0][3] = "Turn left at the next exit";
  7.  
  8. $rows[1][0] = "You gave me life";
  9. $rows[1][1] = "Will you be my friend";
  10. $rows[1][2] = "I like you more everyday";
  11. $rows[1][3] = "Is that a wig on your head";
  12.  
  13. $q = "a b c d";
  14. $q_array = explode(" ", $q);
  15. $q_array = array_unique($q_array);
  16. $q_array = array_values($q_array);
  17.  
  18. $num_q = count($q_array);
  19.  
  20. foreach ($q_array as $key) $q_array1[] = "!($key)!i";
  21.  
  22. foreach($rows as $key){
  23. $row .= "<tr>";
  24. foreach($key as $str){
  25. $str = preg_replace($q_array1, "<b>$1</b>", $str);
  26. $row .= "<td>$str</td>";}
  27. $row .= "</tr>";
  28. }
  29.  
  30. echo "<table border=\"1\">$row</table>";
  31.  
  32. print_r($q_array1);
  33.  
  34. ?>
Last edited by shaneog; Jan 10th, 2009 at 9:50 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shaneog is offline Offline
10 posts
since Jan 2009
Jan 10th, 2009
0

Smaller example of problem...

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"

php Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. $row = "You are the best";
  4.  
  5. $q = "a b";
  6.  
  7. $q_array = explode(" ", $q);
  8.  
  9. foreach ($q_array as $key) $q_array1[] = "!($key)!i";
  10.  
  11. $str = preg_replace($q_array1, "<b>$1</b>", $row);
  12.  
  13. echo $str;
  14.  
  15. ?>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shaneog is offline Offline
10 posts
since Jan 2009
Jan 10th, 2009
0

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

What about something like:
php Syntax (Toggle Plain Text)
  1. <?php
  2. function makeBold($var){
  3. return (ereg_replace("[a|b]+[a-z]+[a-z]","<b>\\0</b>",$var));
  4. }
  5. $row = "You are the best";
  6. echo makeBold($row);
  7. ?>
Outputs: You <b>are</b> the <b>best</b>
Last edited by buddylee17; Jan 10th, 2009 at 12:39 pm.
Reputation Points: 232
Solved Threads: 137
Practically a Master Poster
buddylee17 is offline Offline
665 posts
since Nov 2007
Jan 10th, 2009
0

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

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???
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shaneog is offline Offline
10 posts
since Jan 2009
Jan 10th, 2009
0

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

Try this one out:
php Syntax (Toggle Plain Text)
  1. <?php
  2. $group =array("A","a","B","b");
  3. $row = "You are the best At BaseBall";
  4. $all = implode("|",$group); // convert the array into a pipe delimited string
  5. echo (ereg_replace($all,"<b>\\0</b>",$row));
  6. ?>
Reputation Points: 232
Solved Threads: 137
Practically a Master Poster
buddylee17 is offline Offline
665 posts
since Nov 2007
Jan 10th, 2009
0

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

Thanks for your help buddylee17! Got it to work case insensitive using eregi_replace.
Last edited by shaneog; Jan 10th, 2009 at 9:06 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shaneog is offline Offline
10 posts
since Jan 2009
Jan 10th, 2009
0

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

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:

php Syntax (Toggle Plain Text)
  1. 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
Last edited by shaneog; Jan 10th, 2009 at 9:36 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shaneog is offline Offline
10 posts
since Jan 2009
Jan 10th, 2009
0

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

It would be a lot more efficient to just use the string replacement functions such as str_replace() or str_ireplace()


PHP Syntax (Toggle Plain Text)
  1. $replace = array('a', 'b');
  2. $replacements = array('<b>a</b>', '<b>b</b>');
  3.  
  4. $subject = "You are the best At BaseBall";
  5.  
  6. $subject= str_ireplace($replace, $replacements, $subject);
  7.  
  8. echo $subject;

or
PHP Syntax (Toggle Plain Text)
  1. $replace = array('a', 'b');
  2.  
  3. $subject = "You are the best At BaseBall";
  4.  
  5. foreach($replace as $x) {
  6. $subject = str_ireplace($x, '<b>'.$x.'</b>', $subject);
  7. }
  8.  
  9. 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
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Calender run on IE But Not On firefox??
Next Thread in PHP Forum Timeline: $PHP_SELF within html form question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC