简体   繁体   中英

Laravel 5.1 Linkification / clickable links in user-entered text

I'm currently working on a Laravel 5.1 project and using the Blade template engine.

Blade offers a syntax that generally escapes all user input like so:

{{ $value }}

I'd now like to escape everything, but URLs from user input when outputting it in order to make links clickable.

For example, the link

http://google.de/ 

should be caught (ie by a certain RegEx) and transformed into:

<a href="http://google.de">http://google.de/</a>

I do have the possibility of doing it in my AppServiceProvider using plain PHP (traverse the text, replace text links with clickable HTML elements and replace everything that is not a link with it's respective HTML-Entity counterparts).

However, since this is a framework and frameworks are designed to lessen the burden of re-inventing the wheel over and over I'd expect there to be some better solution than doing what I'd do in every other PHP script.

I don't know if I am getting it clear, but

{!! $value !!}  

will prevent the default blade escape. So, if within your blade view you have a $link variable equals to http://google.de/ , you can surely show it unescaped:

<a href="{!! $link !!}">{!! $link !!}</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM