Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 拉威尔内部链接_Laravel_Laravel 5 - Fatal编程技术网

Laravel 拉威尔内部链接

Laravel 拉威尔内部链接,laravel,laravel-5,Laravel,Laravel 5,我正在寻找一些代码的例子,使关键字从张贴到内部链接 在post表中,我有body属性,其中是post的文本。我制作了额外的表“关键字”,其中是单词和URL属性 所以我需要的脚本将自动识别这个词是在表中添加“关键字”,并找到这个词在后文,并把这个词的超链接。你们能帮帮我吗 use App\Post; use App\Category; use App\Keyword; public function getSingle($slug) { // fetch from the DB bas

我正在寻找一些代码的例子,使关键字从张贴到内部链接

在post表中,我有body属性,其中是post的文本。我制作了额外的表“关键字”,其中是单词和URL属性

所以我需要的脚本将自动识别这个词是在表中添加“关键字”,并找到这个词在后文,并把这个词的超链接。你们能帮帮我吗

use App\Post;
use App\Category;
use App\Keyword;


public function getSingle($slug) {
    // fetch from the DB based on slug
    $categoriesall = Category::all();
    $post = Post::where('slug', '=', $slug)->first();

    // return the view and pass in the post object
    return view('news.single')->withPost($post)->withCategoriesall($categoriesall);
}

这只是一个起点,无论你是建设

public function getSingle($slug) {
    $keywords = Keyword::get()->toArray();
    $categoriesall = Category::all();
    $post = Post::where('slug', '=', $slug)->first();

    $post->text = $this->transform($keywords, $post->text);
    return view('news.single', compact('post', 'categoriesall'));
}

private function transform($keywords, $text)
{
     //takes 2 parameters
     //1st parameter is keywords array
     //2nd parameter is text to be transformed

     $words = explode(" ", $text); //split wherever we have space 

     foreach( $words as $key => $value ) {
         if ( in_array($value, $keywords) ) {
             $words[$key] = "<a href="/search?keywords=" . $value . ">. $value .</a>";
         }
     }

     $paragraph = implode(" ", $words);
     return $paragraph;
}
公共函数getSingle($slug){
$keywords=关键字::get()->toArray();
$categoriesall=Category::all();
$post=post::其中('slug','=',$slug)->first();
$post->text=$this->transform($keywords,$post->text);
返回视图('news.single',compact('post','categoriesall');
}
私有函数转换($keywords,$text)
{
//获取2个参数
//第一个参数是关键字数组
//第二个参数是要转换的文本
$words=explode(“,$text);//在有空间的地方拆分
foreach($key=>$value的单词){
if(在数组中($value,$keywords)){
$words[$key]=“”;
}
}
$段落=内爆(“,$字);
返回$段;
}
请记住,这是一个起点,因为此解决方案根本没有经过优化。e、 g:如果您的文本包含html标记,这些标记可能会转换为锚,因为该算法使用空格作为分隔符;大多数情况下,html中都有空格