hi have the next code

$search="ala salsa portocala nueve vacas";
$where="texto ala salsa nueve texto portocala verde nueve";
$search = explode(" ",$search);
$old_kw = array($search['0'], $search['1'], $search['2'], $search['3']);
$new_kw = array('<b>'.$search['0'].'</b>', '<b>'.$search['1'].'</b>', '<b>'.$search['2'].'</b>', '<b>'.$search['3'].'</b>');
$where = str_replace($old_kw, $new_kw, $where);
echo $where;

because i just started to learn php there are some things wich i don't understand very well.

my problem is the $search because the number of elements in array can vary.
in the case there are more than 4 elements everything but my problem came when i have 1-3 elements in array because i get a notice with "Notice: Undefined index: 2 or 3 etc... "

how could make $old_kw to be created in function of count($search)
so if the count($search) is 10...
$old_kw to be array($search etc etc $search and not defined by me manualy


Hello yli!

Are you trying to replace values or strings? In your example, you're using str_replace() which gives us interesting results. For instance: "portocala" becomes "portoc<b>ala</b>"

For THIS example, I'll assume that's what you're expecting! :)

You can replace your new_kw/old_kw logic with a simple loop, which will loop through that array, no matter how many elements there are:


$search = "ala salsa portocala nueve vacas";
$where  = "texto ala salsa nueve texto portocala verde nueve";

$old_kw = explode(" ",$search);

foreach ($old_kw as $key => $value) {
   $new_kw[$key] = "<b>$value</b>";

$where  = str_replace($old_kw, $new_kw, $where);

echo "$where\n";


I get the following output, which is identical to what I was getting from the original script:

texto <b>ala</b> <b>salsa</b> <b>nueve</b> texto portoc<b>ala</b> verde <b>nueve</b>

I hope this helps! Let us know if this isn't what you were looking for, and we'll see what we can do :)

Member Avatar
  function strong($n){
	return "<strong>$n</strong>";  
  	$search="ala salsa portocala nueve vacas";
	$where="texto ala salsa nueve texto portocala verde nueve";
	$search_r = array_unique(explode(" ",$search));
  	$where_r = array_unique(explode(" ",$where));
  	$common = array_intersect($search_r,$where_r);
	$output = str_replace($common,array_map("strong",$common),$where);
   echo $output;



texto <strong>ala</strong> <strong>salsa</strong> <strong>nueve</strong> texto portoc<strong>ala</strong> verde <strong>nueve</strong>

Note that this changes ala at the end of portocala as well - if you don't want this, perhaps use preg_replace() and regex using "/\b...\b/"

hi. all thanks to both.
i tried to make something like gromit but something went wrong and did not worked. but this is fine. this serve me to lear what i made wrong.

and about ardav also is nice. i just learned something new. i will study this array_intersect and array_unique to see what they do.

thanks again

Glad we could help! ardav's solution is definitely more elegant :)
My goal was to stay as true to your original script as possible.

Thanks for the feedback, and good luck!

Member Avatar

Thanks gromit, we'll slap each other's backs again. I thought yours was simpler and easier to understand. Sorry I was working on it while you posted, otherwise I probably wouldn't have bothered. It would be interesting to see, which was quickest. I know loops take time, but so do array functions.


@yli. if you think this is solved, mark it so with the link below.

in my case i used the gromit solution because used in my script it take me only 3 lines.

Member Avatar

Fair one...
Forcing my code into 3 lines:

function strong($n){return "<strong>$n</strong>";}
$common = array_intersect(array_unique(explode(" ",$search)),array_unique(explode(" ",$where)));
echo str_replace($common,array_map("strong",$common),$where);