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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
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.