raman Posted December 24, 2008 Share Posted December 24, 2008 I have an array $het which consists of elements like >seq1 DDDDDDHJJJJJJJJJJJJJJJJJJKNHJHHGHFGJFGJFGJFGJFGJF G >seq2 HFGJFGJFGJFGWEHFFHFGFHGHFGHFGJFGJFGJFG And I have another array $try which has elements like DDE HHY Now in each of the elements of the first array I want to highlight all the elements of the second array with another color, using preg_replace. my script is: $try=preg_split('/\n/',$filee,-1,PREG_SPLIT_NO_EMPTY); //echo $try[1];echo"\n"; $het = preg_split( '/>.*\n/', $file_data, -1, PREG_SPLIT_NO_EMPTY ); //echo $het[0]; foreach($het as $entry){ foreach ($try as $motiv){ $seq_colord=preg_replace("/$motiv/", "<BIG><font color=blue><b>|</b></font><font color=red>$motiv</font><font color=blue><b>|</b></font></BIG>", $entry); echo "$seq_colord";echo"--------"; } } Here in the $seq_colord, I want all the replacements of $motiv at the same time, but I get only one $motiv replaced and all $seq_colord displayed separately. can someone say how it can be done in one go ? eg. if I want to highlight AAA and DDD both with the red color at the same time in AFAAAAHHHHDDDDHGGGAAA. Quote Link to comment https://forums.phpfreaks.com/topic/138286-solved-preg_replace-function/ Share on other sites More sharing options...
RussellReal Posted December 24, 2008 Share Posted December 24, 2008 there is a much simpler way.. <? $het = new Array( "hello how are you?", "I said \"howdy partner\" and he said \"HELLO FRIEND!\"" ); $highlightArray = new Array("hello","howdy"); $with = new Array(); foreach ($highlightArray as $k => $v) { $with[$k] = "<b>".$v."</b>"; } $het = str_ireplace($highlightArray,$with,$het); ?> Quote Link to comment https://forums.phpfreaks.com/topic/138286-solved-preg_replace-function/#findComment-723019 Share on other sites More sharing options...
raman Posted December 24, 2008 Author Share Posted December 24, 2008 Thanks it does solve my problem, but I had to make a change in the code you gave, and that was with the declaration of the array, $with=new array(); gives a syntax error whereas $with=array(); works alright. Quote Link to comment https://forums.phpfreaks.com/topic/138286-solved-preg_replace-function/#findComment-723038 Share on other sites More sharing options...
RussellReal Posted December 24, 2008 Share Posted December 24, 2008 oh sorry, js and php mixup there CANT KEEP IT ALL IN ITS PLACE IN THE WONDERFUL MIND OF RUSSELL Quote Link to comment https://forums.phpfreaks.com/topic/138286-solved-preg_replace-function/#findComment-723044 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.