user2726957
user2726957

Reputation: 179

search in words on an string and change formatting

I wan't to create a highlight tag search function via php

when I search a part of word...whole of word be colored

for example this is a sample text:

Text: British regulators say traders used private online chatrooms to coordinate their buying and selling to shift currency prices in their favor.

when I search "th" the output be like this:

Text: British regulators say traders used private online chatrooms to coordinate their buying and selling to shift currency prices in their favor.

So...I tried this code...please help me to complete it.

This is a algorithm:

$text= "British regulators say...";
foreach($word in $text)
{
  if( IS There "th" in $word)
   {
      $word2= '<b>'.$word.'</b>'
      replace($word with word2 and save in $text) 
   }
}

how can I it in php language?

Upvotes: 4

Views: 84

Answers (5)

Hanky Panky
Hanky Panky

Reputation: 46900

function highLightWords($string,$find)
{
   return preg_replace('/\b('.$find.'\w+)\b/', "<b>$1</b>", $string); 
}

Usage:

$string="British regulators say traders used private online chatrooms to coordinate their buying and selling to shift currency prices in their favor.";
$find="th";
print_r(highLightWords($string,$find));

Fiddle

Edit after your comment:

...How can I do it for middle characters? for example "line"

Very easy, just update the regex pattern accordingly

return preg_replace("/\b(\w*$find\w*)\b/", "<b>$1</b>", $string); 

Fiddle

Upvotes: 2

Mahendra Jella
Mahendra Jella

Reputation: 5596

You can go with the following code

   <?php

    $string = "British regulators say traders used private online chatrooms to coordinate their buying and selling to shift currency prices in their favor";

     $keyword = "th";
     echo highlightkeyword($string , $keyword );

    function highlightkeyword($str, $search) {
        $occurrences = substr_count(strtolower($str), strtolower($search));
        $newstring = $str;
        $match = array();

        for ($i=1;$i<$occurrences;$i++) {
            $match[$i] = stripos($str, $search, $i);
            $match[$i] = substr($str, $match[$i], strlen($search));
            $newstring = str_replace($match[$i], '[#]'.$match[$i].'[@]', strip_tags($newstring));
        }

        $newstring = str_replace('[#]', '<b>', $newstring);
        $newstring = str_replace('[@]', '</b>', $newstring);
        return $newstring;

    }

    ?>

Check here https://eval.in/220395

Upvotes: 1

malar
malar

Reputation: 126

Use strpos() to find the position of the character you search for.. Then start reading from that identified position of character to till you don't find any space..

Upvotes: 2

Marco Mura
Marco Mura

Reputation: 582

Let's say a lot of things.

First, as you know php is a server-side code, so, as long as you won't mind reload the page each time or use ajax...

The correct way i think will be using Javascript to Achieve this.

That said to explode the text you need to use another function, to be sure of what obtained:

Something like:

$str = "Hello world. It's a beautiful day.";
$words = explode(" ",$str);

Now Words var will contain the exploded string.

Now you can loop and replace (for example) and then re-construct the string and print it or do other.

Upvotes: 1

AbraCadaver
AbraCadaver

Reputation: 78994

Should be much easier:

$word = "th";
$text = preg_replace("/\b($word.*?)\b/", "<b>$1</b>", $text);

Upvotes: 1

Related Questions