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

Reply

Join Date: Jan 2009
Posts: 10
Reputation: shaneog is an unknown quantity at this point 
Solved Threads: 0
shaneog shaneog is offline Offline
Newbie Poster

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

 
0
  #1
Jan 10th, 2009
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???

  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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 10
Reputation: shaneog is an unknown quantity at this point 
Solved Threads: 0
shaneog shaneog is offline Offline
Newbie Poster

Smaller example of problem...

 
0
  #2
Jan 10th, 2009
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"

  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. ?>
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 596
Reputation: buddylee17 has a spectacular aura about buddylee17 has a spectacular aura about 
Solved Threads: 125
buddylee17's Avatar
buddylee17 buddylee17 is offline Offline
Posting Pro

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

 
0
  #3
Jan 10th, 2009
What about something like:
  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.
Lost time is never found again.
- Benjamin Franklin
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 10
Reputation: shaneog is an unknown quantity at this point 
Solved Threads: 0
shaneog shaneog is offline Offline
Newbie Poster

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

 
0
  #4
Jan 10th, 2009
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???
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 596
Reputation: buddylee17 has a spectacular aura about buddylee17 has a spectacular aura about 
Solved Threads: 125
buddylee17's Avatar
buddylee17 buddylee17 is offline Offline
Posting Pro

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

 
0
  #5
Jan 10th, 2009
Try this one out:
  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. ?>
Lost time is never found again.
- Benjamin Franklin
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 10
Reputation: shaneog is an unknown quantity at this point 
Solved Threads: 0
shaneog shaneog is offline Offline
Newbie Poster

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

 
0
  #6
Jan 10th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 10
Reputation: shaneog is an unknown quantity at this point 
Solved Threads: 0
shaneog shaneog is offline Offline
Newbie Poster

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

 
0
  #7
Jan 10th, 2009
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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

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

 
0
  #8
Jan 10th, 2009
It would be a lot more efficient to just use the string replacement functions such as str_replace() or str_ireplace()


  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
  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
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC