Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/138286-solved-preg_replace-function/
Share on other sites

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);
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.