asmith Posted October 3, 2008 Share Posted October 3, 2008 hey guys I have an array of values and I want to bold all values in the $content. each time this array changes, can contain 20 values, or sometime 0 . how do i write this with preg_replace? Quote Link to comment https://forums.phpfreaks.com/topic/126870-solved-bolding-array-values/ Share on other sites More sharing options...
DarkWater Posted October 3, 2008 Share Posted October 3, 2008 Now, do you only wants it to be replaced when it's actually its own word, or does it not matter where in a word it is? I.E: $content = array('test'); $string = "We have a new contestant!"; //replace stuff echo $string; And it echos: We have a new con<b>test</b>ant! Is that what you want? Quote Link to comment https://forums.phpfreaks.com/topic/126870-solved-bolding-array-values/#findComment-656245 Share on other sites More sharing options...
asmith Posted October 3, 2008 Author Share Posted October 3, 2008 yes, thanks array("test","mountain","php"); <b>test<b>,<b>mountain</b>,<b>php</b> the array is a variable that can have changeable number of values. but your pattern is what i want. Quote Link to comment https://forums.phpfreaks.com/topic/126870-solved-bolding-array-values/#findComment-656251 Share on other sites More sharing options...
CroNiX Posted October 3, 2008 Share Posted October 3, 2008 I realize that you posted this in regex, but if you just want to bold each element in the array you could: <?php $testarray = array('this', 'will', 'be', 'bolded'); $boldarray = array_map('boldarray', $testarray); function boldarray($el){ return '<b>' . $el . '</b>'; } print_r($boldarray); Quote Link to comment https://forums.phpfreaks.com/topic/126870-solved-bolding-array-values/#findComment-656509 Share on other sites More sharing options...
The Little Guy Posted October 3, 2008 Share Posted October 3, 2008 $words = array("test","mountain","php"); $string = 'this is a test string, about a php mountain'; echo preg_replace('~\b('.implode('|', $words).'[a-zA-Z]{0,3}[0-9]{0,3})\b(?![^<]*[>])~is','<strong>$0</strong>',$string); Quote Link to comment https://forums.phpfreaks.com/topic/126870-solved-bolding-array-values/#findComment-656524 Share on other sites More sharing options...
asmith Posted October 3, 2008 Author Share Posted October 3, 2008 Thanks little guy just one thing. your code does not fint the words this : $string = 'this is a test string, about a thephp mountains'; When they are sticked after or before or in a word. (I made those letter italic) Quote Link to comment https://forums.phpfreaks.com/topic/126870-solved-bolding-array-values/#findComment-656533 Share on other sites More sharing options...
The Little Guy Posted October 3, 2008 Share Posted October 3, 2008 How about this: $words = array("test","mountain","php"); $string = 'this is a test string, about a thephp mountain'; echo preg_replace('~('.implode('|', $words).')~','<strong>$0</strong>',$string); Quote Link to comment https://forums.phpfreaks.com/topic/126870-solved-bolding-array-values/#findComment-656543 Share on other sites More sharing options...
asmith Posted October 3, 2008 Author Share Posted October 3, 2008 Thanks little guy, Thank you very much ^^ Quote Link to comment https://forums.phpfreaks.com/topic/126870-solved-bolding-array-values/#findComment-656555 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.