mardi 4 avril 2017

Laravel internal links with preg_replace

I'm trying to make with Laravel internal links witch will be recolonize with keywords from database.

public function getSingle($slug) {

    $post = Post::where('slug', '=', $slug)->first();

    $keyword = Keyword::all();
    $data = array();

    foreach($keyword as $word){
             $data = $word->keyword;
             $sentence = preg_replace('@(?<=\W|^)('.$data.')(?=\W|$)@i', '<a href="'.$word->url.'">$1</a>', $post->body);
    }


    return view('casino-news.single')->withPost($post)->withSentence($sentence);
}

This code work fine, but i have problem with for each loop because it show only one resolute of keyword from database. I try to add array variable but it's same. So i need fix witch show multiple keywords not only one.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire