Jump to content

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

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.