Search code examples
phplaravellaravel-blade

Display the searched characters with coloring [Laravel10]


I'm trying to display the searched characters with yellow coloring.

I searched web and Here is my current code.

I'm trying to display the searched characters with yellow coloring.

I searched web and Here is my current code.

I can search keywords and get result.

but colors function are not working.

I'm guessing the code of blade? but not sure. Could you teach me what is causing please?

Blade


@foreach ($posts as $post)
<tr>                           
    <td>{{ $post->title }}</td>                            
    <td>{{ $result }}</td
<tr>    
@endforeach
 

**Controller **


$keyword = $request->input('search'); 
    $q = Post::query();     
  
    if(isset($request->keyword) && $request->keyword_type === "or"){
        $keywords = explode(",", $request->keyword);
        $q->where(function ($query) use($keywords) {
            foreach($keywords as $keyword){
                $query->orWhere('title', 'LIKE',"%$keyword%");
            }
        });
    } elseif(isset($request->keyword)) {
        $keywords = explode(",", $request->keyword);
        foreach($keywords as $keyword){
            $q->where('title', 'LIKE',"%$keyword%");
        }
    }

        // \DB::enableQueryLog();
        $results = $q->get();
        // dd(\DB::getQueryLog());

        $results->count=count($results);

    // Highlisght strings area 
        if(isset($request->keyword)) {
            foreach($results as $result){
                $keywords = explode(",", $request->keyword);
                $result->title = $this->search_text_highlight($keywords, $result->title);
            }
        }
        $results->count=count($results);

    $count_result = count($posts);
  


    return view('posts.index')
    ->with([
        'posts' => $posts,         
        'result' => $result->title  
    ])->with('i', (request()->input('page', 1) - 1) * 5);
    
}

/**
 * function find keyword if finds make it yellow highlight 
 *
 * @param array $keywords 
 * @param string $target_string 
 * @return string $target_string
 */
public function search_text_highlight($keywords, $target_string)
{
    // if target is empty return the string 
    if(empty($keywords)){
        return $target_string;
    }

    foreach($keywords as $keyword){
        // if find keyword highlight and return 
        if( ($pos = mb_strpos($target_string, $keyword, 0, 'UTF-8')) !== false ){
            // get keyword from text 
            $str = mb_substr($target_string, $pos, mb_strlen($keyword, 'UTF-8'), 'UTF-8');
            // make yellow highlight color 
            $target_string = str_replace($str, "<span style='background-color:yellow'>{$str}</span>", $target_string);
        }
    }
    return $target_string;
}

Solution

  •  $results = $q->get();
    
        $highlightedResults = [];
        foreach ($results as $result) {
            $keywords = explode(",", $request->keyword);
            $highlightedTitle = $this->search_text_highlight($keywords, $result->title);
            $highlightedResults[] = [
                'post' => $result,
                'highlightedTitle' => $highlightedTitle,
            ];
        }
    
        return view('posts.index', [
            'highlightedResults' => $highlightedResults,
        ])->with('i', (request()->input('page', 1) - 1) * 5);
    

    Blade file

    @foreach ($highlightedResults as $highlightedResult)
        <div>
            <h2>{{ $highlightedResult['highlightedTitle'] }}</h2>
            <p>{{ $highlightedResult['post']->tag }}</p>
            <p>{{ $highlightedResult['post']->description }}</p>
        </div>
    @endforeach