从 Laravel Blade 上的字符串中删除 HTML 标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36247382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove HTML tags from Strings on laravel blade
提问by hassen zouari
I want remove HTML tags (all) from a string on laravel blade ...
我想从 laravel 刀片上的字符串中删除 HTML 标签(全部)...
code
代码
{!! \Illuminate\Support\Str::words($subject->body, 5,'...') !!}
output (example)
输出(示例)
<p>hassen zouari</p>
I want that it be like this
我希望它是这样的
hassen zouari
回答by Alexey Mezenin
Try to use strip_tags()
function:
尝试使用strip_tags()
功能:
http://php.net/manual/en/function.strip-tags.php
http://php.net/manual/en/function.strip-tags.php
Update:Try to do something like this in a controller:
更新:尝试在控制器中做这样的事情:
$taglessBody = strip_tags($subject->body);
Then pass this variable into a blade template and use it instead of $subject->body
.
然后将此变量传递到刀片模板并使用它而不是$subject->body
.
回答by Sahith Vibudhi
You can use strip_tags($yourString); to strip the html tags. In blade you could achieve this by
您可以使用 strip_tags($yourString); 去除html标签。在刀片中,您可以通过
{{ strip_tags($yourString) }}
//if your string is <h1> my string </h1>
//output will be my string.
hope that is helpful :)
希望有帮助:)
回答by Paul Basenko
As for me, I use this construction:
至于我,我使用这种结构:
{!! str_limit(strip_tags($post->text), $limit = 50, $end = '...') !!}
I hope, my code was helpful for somebody)
我希望,我的代码对某人有帮助)
回答by upendra
You can use
您可以使用
{{ strip_tags( $value->description ) }}
回答by Wahsei
Add the below code into your helpers
将以下代码添加到您的助手中
if (! function_exists('words')) {
/**
* Limit the number of words in a string.
*
* @param string $value
* @param int $words
* @param string $end
* @return string
*/
function words($value, $words = 100, $end = '...')
{
return \Illuminate\Support\Str::words($value, $words, $end);
}
}
& use this in your blade
并在您的刀片中使用它
{{ words($sentence, $limit = 20, $end = ' ..... more') }}
回答by Carsim Rheedwan Adedotun Holuw
just by doing this {!! $value !!}
will solve your problem
这样做{!! $value !!}
就能解决你的问题
回答by Lalit Baghel
you must know about the diffrence between {{ }}
and {!! !!}
你必须知道{{ }}
和之间的区别{!! !!}
1.1) laravel have predefined you can load variable exact value by using {{$a }}
1.1)laravel 已经预定义你可以使用加载变量精确值 {{$a }}
or
或者
1.2)laravel loading with strip remove {!! $a !!}
1.2)laravel 加载带条删除 {!! $a !!}
please take it seriously this qustion answer its protect your website from cross site scripting excecution.for example if user save his name but he puted script alert.then if you using core php and not handling the strip tags then this script excution when page laod
请认真对待这个问题的答案,它可以保护您的网站免受跨站点脚本执行。例如,如果用户保存了他的名字但他放置了脚本警报。那么如果您使用核心 php 并且不处理带标签,那么当页面加载时这个脚本执行
so i give you some more harmful example 1)like i puted my name as script or in a about section and in script i write some code who get the browser cookies of open website and curl request for saving on my server.
所以我给你一些更有害的例子 1)就像我把我的名字作为脚本或关于部分和脚本我写了一些代码来获取打开网站的浏览器 cookie 和 curl 请求以保存在我的服务器上。
so you understand this whats happen if i got the each user cookies by curl and i just puted a cross script in about filed and its saved in database.
所以你明白如果我通过 curl 获取每个用户的 cookie 会发生什么,我只是在 about 文件中放置了一个交叉脚本并将其保存在数据库中。
回答by Swapnil Nath
use this in your code
在您的代码中使用它
$allcms = json_decode(strip_tags(Cms::where('status','active')->get()),true);
$allcms = json_decode(strip_tags(Cms::where('status','active')->get()),true);
return response()->json(['status' => 'success','message' =>'All CMS','data' => $allcms]);