如何使用字符串限制解码html特殊字符?

时间:2019-05-14 16:42:12

标签: php laravel

如何解码HTML特殊字符和字符串限制?

此行不起作用:

<p>{!!\Str::limit($event->description,180,"..")!!}<a href="#">View Detail</a></p>

但是如果我正常地将其用于字符串限制,它将:

<p>{{\Str::limit($event->description,180,"..")}}<a href="#">View Detail</a></p>

上面的行适用于字符串限制,但不能解码。

1 个答案:

答案 0 :(得分:0)

目前尚不清楚$event->description包含什么。另外,还不清楚\Str代表什么,甚至不清楚您使用的是哪个版本的Laravel。以下示例假定您的意思是Illuminate\Support\Str,并且在版本5.8.XX上,并且我包括两个不同的描述变量。

$description = "<p><strong>Lorem ipsum</strong> dolor sit amet, consectetur adipiscing elit. &quot;Ut augue orci, pellentesque at metus sed, tempor ultrices diam.&quot; Quisque tempor ullamcorper nunc, vitae efficitur justo tristique sed. Sed ac orci condimentum, viverra mi quis, pharetra sapien. Suspendisse nec nisi sem. </p>\n";

$descEncoded = "&lt;p&gt;&lt;strong&gt;Lorem ipsum&lt;/strong&gt; dolor sit amet, consectetur adipiscing elit. &amp;quot;Ut augue orci, pellentesque at metus sed, tempor ultrices diam.&amp;quot; Quisque tempor ullamcorper nunc, vitae efficitur justo tristique sed. Sed ac orci condimentum, viverra mi quis, pharetra sapien. Suspendisse nec nisi sem. Sed porta finibus posuere. Nunc a libero vitae mi ornare interdum.&lt;/p&gt;";

html_entity_decode:

{!!\Illuminate\Support\Str::limit(html_entity_decode($description),180,"...")!!}
{!!\Illuminate\Support\Str::limit(html_entity_decode($descEncoded),180,"...")!!}

html_entity_decode output

htmlspecialchars_decode:

{!!\Illuminate\Support\Str::limit(htmlspecialchars_decode($description),180,"...")!!}
{!!\Illuminate\Support\Str::limit(htmlspecialchars_decode($descEncoded),180,"...")!!}

htmlspecialchars_decode output

希望这对您的事业有所帮助。如果您更详细地更新问题,我可以根据需要调整答案。

相关问题