| | |
Trying to BOLD characters in string w/ preg_replace from multi-dimensional array???
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jan 2009
Posts: 10
Reputation:
Solved Threads: 0
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???
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)
<?php $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>"; print_r($q_array1); ?>
Last edited by shaneog; Jan 10th, 2009 at 9:50 am.
•
•
Join Date: Jan 2009
Posts: 10
Reputation:
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 <b>ab>re the best"
Here is another broken down example... it outputs:
"You <b>ab>re the best"
php Syntax (Toggle Plain Text)
<?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; ?>
Re: Trying to BOLD characters in string w/ preg_replace from multi-dimensional array???
0
#3 Jan 10th, 2009
What about something like: Outputs: You <b>are</b> the <b>best</b>
php Syntax (Toggle Plain Text)
<?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); ?>
Last edited by buddylee17; Jan 10th, 2009 at 12:39 pm.
Lost time is never found again.
- Benjamin Franklin
- Benjamin Franklin
•
•
Join Date: Jan 2009
Posts: 10
Reputation:
Solved Threads: 0
Re: Trying to BOLD characters in string w/ preg_replace from multi-dimensional array???
0
#4 Jan 10th, 2009
Re: Trying to BOLD characters in string w/ preg_replace from multi-dimensional array???
0
#5 Jan 10th, 2009
Try this one out:
php Syntax (Toggle Plain Text)
<?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)); ?>
Lost time is never found again.
- Benjamin Franklin
- Benjamin Franklin
•
•
Join Date: Jan 2009
Posts: 10
Reputation:
Solved Threads: 0
Re: Trying to BOLD characters in string w/ preg_replace from multi-dimensional array?
0
#6 Jan 10th, 2009
•
•
Join Date: Jan 2009
Posts: 10
Reputation:
Solved Threads: 0
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:
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
For example, if I want to replace characters in an array I need to use:
php Syntax (Toggle Plain Text)
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.
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()
or
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
PHP Syntax (Toggle Plain Text)
$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
PHP Syntax (Toggle Plain Text)
$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
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
Other Threads in the PHP Forum
- Previous Thread: how to insert email into database
- Next Thread: $PHP_SELF within html form question
| Thread Tools | Search this Thread |
apache api array beginner binary broken cache cakephp checkbox class cms code confirm cron curl customizableitems database date display dynamic echo email error external fcc file files folder form forms forum freelancing function functions google header headmethod howtowriteathesis href htaccess html iframe image include insert ip javascript joomla limit link login mail malfunction menu method mlm mod_rewrite multiple mysql neutrality oop pageing pagerank paypal pdf php phpmysql play problem query question radio random recursion remote root script search select server sessions sms soap source space sql support! syntax system table template tutorial update upload url validator variable video web youtube






