skip to Main Content

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

2

Answers


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

    try with this code. Make sure to use {!! $result !!} instead of {{ $result }} to render the HTML as it is, allowing the tags for highlighting to take effect.

    Login or Signup to reply.
  2.  $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);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search