Php 如何在laravel模型中从html中获取纯文本?
我有一个名为Php 如何在laravel模型中从html中获取纯文本?,php,string,laravel,eloquent,tinymce,Php,String,Laravel,Eloquent,Tinymce,我有一个名为description的数据库列,其中保存了来自tinymce编辑器的输入。数据是这样的 <p><strong>Paragliding </strong></p> <p>Paragliding is the recreational and competitive adventure sport of flying paragliders: lightweight, free-flying, foot-launched gl
description
的数据库列,其中保存了来自tinymce编辑器的输入。数据是这样的
<p><strong>Paragliding </strong></p>
<p>Paragliding is the recreational and competitive adventure sport of flying paragliders: lightweight, free-flying, foot-launched glider aircraft with no rigid primary structure. The pilot sits in a harness suspended below a fabric wing.<br /> </p>
但是,我需要一些处理来从文本中获得30个单词,模型中的代码如下
/**
* @param $sentence
* @param int $count
* @return mixed
*/
function get_words($sentence, $count = 30) {
preg_match("/(?:\w+(?:\W+|$)){0,$count}/", $sentence, $matches);
return $matches[0];
}
/**
* @return short_description
*/
public function shortDescription()
{
return $this->get_words($this->description);
}
当我调用shortDescription
函数时,我当前正在清空
有没有办法从$this->description
变量获取纯文本
非常感谢您的任何建议和帮助。您可以使用strip_tags()php函数获取纯文本
$text = "<p><strong>Paragliding </strong></p>
<p>Paragliding is the recreational and competitive adventure sport of flying paragliders: lightweight, free-flying, foot-launched glider aircraft with no rigid primary structure. The pilot sits in a harness suspended below a fabric wing.<br /> </p>";
$plainText = strip_tags($text);
$text=“滑翔伞
滑翔伞是飞行滑翔伞的娱乐性和竞争性冒险运动:重量轻、自由飞行、脚踏式滑翔机,没有刚性的主要结构。飞行员坐在悬挂在织物机翼下的安全带中。
”;
$plainText=带标签($text);
产出
“滑翔伞是一种娱乐性和竞争性的冒险活动
飞行滑翔伞运动:轻巧、自由飞行、脚部发射
无刚性主结构的滑翔机。飞行员坐在空中
悬挂在织物翼下的安全带。”
你可以看到更多的细节
或
在5.8版+删除了str帮助程序的情况下,您必须使用illighted\Support\str::limit($string)
而不是str\u limit
或install composer require laravel/helpers
才能使用此功能
{{substr(strip_tags($item->description),0,5)}} //this will print 30 character
{{strip_tags(Illuminate\Support\Str::limit($item->description,30))}} //this will print 30 character
{{strip_tags(Illuminate\Support\Str::words($item->description,30))}} //this will print 30 words
在这里你可以得到所有的字符串助手
让我先试试,我在这里有一段时间不舒服:D,现在我明白了
{{strip_tags(Illuminate\Support\Str::words($item->description,30))}}
//this will print 30 words
{!! html_entity_decode($data->description) !!}
//this will print with html
{strip_tags($data->description)}
//this will strip your HTML tags
{ Illuminate\Support\Str::limit($data->description,30)}} // this will print 30 character with 3 dots default
{{substr(strip_tags($item->description),0,5)}} //this will print 30 character
{{strip_tags(Illuminate\Support\Str::limit($item->description,30))}} //this will print 30 character
{{strip_tags(Illuminate\Support\Str::words($item->description,30))}} //this will print 30 words