Laravel 5 带preg_替换的Laravel内部链接

Laravel 5 带preg_替换的Laravel内部链接,laravel-5,laravel-4,Laravel 5,Laravel 4,我试图使与拉威尔内部链接,将重新与数据库关键字 public function getSingle($slug) { $post = Post::where('slug', '=', $slug)->first(); $keyword = Keyword::all(); $data = array(); foreach($keyword as $word){ $data = $word->keyword;

我试图使与拉威尔内部链接,将重新与数据库关键字

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('news.single')->withPost($post)->withSentence($sentence);
}
公共函数getSingle($slug){
$post=post::其中('slug','=',$slug)->first();
$keyword=keyword::all();
$data=array();
foreach($word作为$word的关键字){
$data=$word->keyword;

$SENTION=preg_replace('@(?这是因为,在每个循环中,您都将句子重置为最后一个。请尝试此操作

public function getSingle($slug) {

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

    $keyword = Keyword::all();
    $data = array();
    $sentence = $post->body;

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


    return view('news.single')->withPost($post)->withSentence($sentence);
}
公共函数getSingle($slug){
$post=post::其中('slug','=',$slug)->first();
$keyword=keyword::all();
$data=array();
$SENTURE=$post->body;
foreach($word作为$word的关键字){
$data=$word->keyword;
$SENTION=preg_replace('@(?)?