Laravel 允许消息链接
Laravel allow links on message
我需要在用户发送消息时允许消息中的链接。我该怎么做?
@foreach($mess as $message)
<li data-mid="{{ $message->id }}">
<div class="row margin-left-none margin-right-none">
<div class="col-md-2 padding-right-none">
<div class="avatar-user text-center">
<img src="{{ $message->sender->avatar }}" alt="$message->sender->name">
</div>
</div>
<div class="col-md-10 padding-left-none">
<div class="user-name">
<span>{{ $message->sender->name }}
@if(Helper::getOnlineUser($message->sender->id))
<i data-toggle="tooltip" title="Онлайн" class="material-icons online">fiber_manual_record</i>
@else
<i data-toggle="tooltip" title="Оффлайн" class="material-icons offline">fiber_manual_record</i>
@endif
<span class="date float-right">{{ $message->created_at->formatLocalized('%H:%m') }}</span></span>
</div>
<div class="message">
{{ $message->body }}
</div>
</div>
</div>
</li>
@endforeach
这是消息视图。
如何允许链接?
示例:消息中的 http://example.com =>
<a href="http://example.com">http://example.com</a>
我需要{!! $消息->正文!!}?或者如何?
在控制器中:
$mess = Chat::conversations($conversation)->for($user)->getMessages($limit, $request->page);
您可以在 blade、
中使用以下 php 函数来完成此操作
<?php
function getDataWithURL($text){
// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// Check if there is a url in the text
if(preg_match($reg_exUrl, $text, $url)) {
// make the urls hyper links
echo preg_replace($reg_exUrl, "<a href=".$url[0].">".$url[0]."</a> ", $text);
} else {
// if no urls in the text just return the text
echo $text;
}
}
?>
你只需要在你的 blade 文件中添加这段代码,然后你就可以像这样检查和替换文本中的链接,
<div class="message">
<?php getDataWithURL($message->body);?>
</div>
希望您能理解。
内置 PHP 函数来处理此问题:
<div class="message">
{!! strip_tags($message->body, '<a>') !!}
</div>
第二个参数是您的白名单。
这样做有以下优点:
- 你没有搞乱正则表达式
- 您仍在使用 blade
{!!!!}
- 您使用的是核心团队开发的经过良好测试的代码
我需要在用户发送消息时允许消息中的链接。我该怎么做?
@foreach($mess as $message)
<li data-mid="{{ $message->id }}">
<div class="row margin-left-none margin-right-none">
<div class="col-md-2 padding-right-none">
<div class="avatar-user text-center">
<img src="{{ $message->sender->avatar }}" alt="$message->sender->name">
</div>
</div>
<div class="col-md-10 padding-left-none">
<div class="user-name">
<span>{{ $message->sender->name }}
@if(Helper::getOnlineUser($message->sender->id))
<i data-toggle="tooltip" title="Онлайн" class="material-icons online">fiber_manual_record</i>
@else
<i data-toggle="tooltip" title="Оффлайн" class="material-icons offline">fiber_manual_record</i>
@endif
<span class="date float-right">{{ $message->created_at->formatLocalized('%H:%m') }}</span></span>
</div>
<div class="message">
{{ $message->body }}
</div>
</div>
</div>
</li>
@endforeach
这是消息视图。
如何允许链接?
示例:消息中的 http://example.com =>
<a href="http://example.com">http://example.com</a>
我需要{!! $消息->正文!!}?或者如何?
在控制器中:
$mess = Chat::conversations($conversation)->for($user)->getMessages($limit, $request->page);
您可以在 blade、
中使用以下 php 函数来完成此操作<?php
function getDataWithURL($text){
// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// Check if there is a url in the text
if(preg_match($reg_exUrl, $text, $url)) {
// make the urls hyper links
echo preg_replace($reg_exUrl, "<a href=".$url[0].">".$url[0]."</a> ", $text);
} else {
// if no urls in the text just return the text
echo $text;
}
}
?>
你只需要在你的 blade 文件中添加这段代码,然后你就可以像这样检查和替换文本中的链接,
<div class="message">
<?php getDataWithURL($message->body);?>
</div>
希望您能理解。
内置 PHP 函数来处理此问题:
<div class="message">
{!! strip_tags($message->body, '<a>') !!}
</div>
第二个参数是您的白名单。
这样做有以下优点:
- 你没有搞乱正则表达式
- 您仍在使用 blade
{!!!!}
- 您使用的是核心团队开发的经过良好测试的代码