Jump to content

highlight words...


redarrow

Recommended Posts

i have tried to highlight words and tried every possable way with php and html

i want to get the words i want to highlight example the words

php

is

redarrow

 

i have tried all the current php conventions..

 

in_array <<<<<<<<,

eregi <<<<<<<<<,,

str_repalce <<<<<<,

implode <<<<,,

explode <<<<<,

loops everythink man how do you do it..........

 

 

the result should be the words wanted to highlight as shown but only

one word will highlight why?

<?php

$message='my name is redarrow and i love php';

$a=array("php","is","redarrow");

for($i=0; $i<count($a); $i++){

$y[]=eregi_replace($a[$i],"<font color='red'>$a[$i]</font>",$message);

foreach ($y as $result){

$r=$result;
}
}

echo $r;

?>

Link to comment
https://forums.phpfreaks.com/topic/96442-highlight-words/
Share on other sites

I'd go for something like this... Maybe it can be cleaned up a bit:

 

<?php

$str = 'my name is redarrow and i love php';
$words = array("php","is","redarrow");
$words2 = array();
foreach($words as $k=>$v)
$words2[$k] = "<font color='red'>".$v."</font>";

echo str_replace($words, $words2, $str);

?>

 

Orio.

Link to comment
https://forums.phpfreaks.com/topic/96442-highlight-words/#findComment-493609
Share on other sites

orio that amazing to me ive been three hours here lol....

 

can you exsplain the empty array...........

 

what it there for..........

 

$words2 = array();<<< this linking

 

to this

$words2[$k] = "<font color='red'>".$v."</font>";

 

and it using the foreach key why....

 

 

Link to comment
https://forums.phpfreaks.com/topic/96442-highlight-words/#findComment-493612
Share on other sites

With preg_replace()... A bit cleaner:

 

<?php

$str = 'my name is redarrow and i love php';
$words = array("php","is","redarrow");
$pattern = "/(". implode("|",$words) .")/";
echo preg_replace($pattern, "<font color='red'>$1</font>", $str);

?>

 

 

First script (with str_replace()), first creates a new array that it's values are the same as those in $words, just they are "wrapped" with the font tags. So then it replaces the matches with the colored words.

The second one (preg_replace()), replaces any occurrence of the words with the same word just "wrapped" with the font tags.

 

Orio.

Link to comment
https://forums.phpfreaks.com/topic/96442-highlight-words/#findComment-493615
Share on other sites

<?php
$str = 'my name is redarrow and i love php. I am miss of the world';
$words = array("php","is","redarrow");
//$pattern = "/(". implode("|",$words) .")/";
$pattern = "/\b(". implode("|",$words) .")\b/";
echo preg_replace($pattern, "<font color='red'>$1</font>", $str);
?>

Link to comment
https://forums.phpfreaks.com/topic/96442-highlight-words/#findComment-493622
Share on other sites

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.