Jump to content

[SOLVED] preg_replace function


raman

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.