Jump to content

Bold Words From Array


The Little Guy

Recommended Posts

I have this regex function to bold words ($words is an array):

 

function boldWords($words, $string){
    return preg_replace('~('.implode('|', $words).')~i','<strong>$0</strong>',$string);
}

 

sometimes there is a "wildcard" input value (asterisk).

 

The current above code takes something like:

new*

 

and bolds: "newest thing ever"

 

I would like it to bold: "newest thing ever"

 

so basically bold the whole word not just part of the word.

Link to comment
https://forums.phpfreaks.com/topic/129682-bold-words-from-array/
Share on other sites

This worked for me:

 

<?php

echo boldWords(array("one", "t*"), "One is before two and three is too.");

function boldWords($words, $string)
{
foreach($words as $k => $w)
{
	$w = '('.str_replace('*', '.*?)', $w, $count);
	if(!$count)
		$w .= ')';
	$words[$k] = $w;
}
$regex = "~".implode('|', $words)."\b~i";

return preg_replace($regex ,'<strong>$0</strong>',$string);
}

?>

 

Enjoy :)

 

Orio.

Sanitize the words with preg_quote.

 

Good point. But that will require a small fix:

 

<?php

echo boldWords(array("one", "t*"), "One is before two and three is too.");

function boldWords($words, $string)
{
   foreach($words as $k => $w)
   {
      $w = '('.str_replace('\*', '.*?)', preg_quote($w), $count);
      if(!$count)
         $w .= ')';
      $words[$k] = $w;
   }
   $regex = "~".implode('|', $words)."\b~i";

   return preg_replace($regex ,'<strong>$0</strong>',$string);
}

?>

 

 

Orio.

Just added some brackets for the ORs:

 

<?php

echo boldWords(array("one", "t*"), "One is before two and three is too.");

function boldWords($words, $string)
{
   foreach($words as $k => $w)
   {
      $w = '('.str_replace('\*', '.*?)', preg_quote($w), $count);
      if(!$count)
         $w .= ')';
      $words[$k] = $w;
   }
   $regex = "~(".implode('|', $words).")\b~i";

   return preg_replace($regex ,'<strong>$0</strong>',$string);
}

?>

 

 

Orio.

I realized, as I was doing searches.... that if I searched for: a**

 

I get this error:

 

Warning: preg_replace() [function.preg-replace]: Compilation failed: unmatched parentheses at offset 11 in /home/.marble/ryannaddy/dudeel.com/incl/functions.php on line 67

 

Warning: preg_replace() [function.preg-replace]: Compilation failed: unmatched parentheses at offset 11 in /home/.marble/ryannaddy/dudeel.com/incl/functions.php on line 67

this should cover the wildcards * and ?:

<?php
echo boldWords(array("o?e", "a**", "t??", "b*e"), "One is before two and three is too.");

function boldWords($words, $string)
{
   $regex = "~\b(?:".preg_replace('/(?:\\\\\*)+/','.*?',str_replace('\?','.',implode('|', array_map('preg_quote',$words)))).")\b~i";
   return preg_replace($regex ,'<strong>$0</strong>',$string);
}
?>

I realized, as I was doing searches.... that if I searched for: a**

 

 

Why would you input something like that? It's like using in SQL "... LIKE 'something%%' ...". I can't see what you're trying to achieve using double wildcards and that's why the function is not build for such input.

 

Orio.

I realized, as I was doing searches.... that if I searched for: a**

 

 

Why would you input something like that? It's like using in SQL "... LIKE 'something%%' ...". I can't see what you're trying to achieve using double wildcards and that's why the function is not build for such input.

 

Orio.

 

like discomatt said, a star is a wildcard that users can enter into a text field. you can try it here: http://dudeel.com/#searchVideo

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.