Milan AM
Milan AM

Reputation: 155

Outputting URL in Laravel blade with anchor tag

In my database, users enter a string of text that could contain a url to some website. Right now I am just outputting that website and the url must be copy and pasted. I am trying to replace that url (if there is one) and convert it to an html anchor tag if there is one.

So far I loop thru every record and replace that text if there is a url in there. (I also want to keep any text they type in around that url.) This is the function I use for that:

foreach ($applications as $a) {
  $web =  $a->websites;

  $web = preg_replace('~(\s|^)(https?://.+?)(\s|$)~im', '$1<a href="$2" target="_blank">$2</a>$3', $web);
  $web = preg_replace('~(\s|^)(www\..+?)(\s|$)~im', '$1<a href="http://$2" target="_blank">$2</a>$3', $web);

  $a->websites = nl2br($web);
}

If I echo and die in the Controller, it properly outputs the text and url with an anchor tag. However in my blade output I do this:

 @foreach ($applications as $application)
<tr>
  <td>{{{ $application->websites or '' }}}</td>
  </td>
</tr>
@endforeach

This is outputting like this though:

<a href="http://ChangeThisToYourSite.com" target="_blank">http://ChangeThisToYourSite.com</a>

(The website link is not being linkifyed, it is just a string.)

Wondering if anyone has any solutions for this.

Upvotes: 0

Views: 2395

Answers (1)

cre8
cre8

Reputation: 13562

By default, Blade {{ }} statements are automatically sent through PHP's htmlentities function to prevent XSS attacks. If you do not want your data to be escaped, you may use the following syntax:

Hello, {!! $name !!}.

http://laravel.com/docs/5.1/blade#displaying-data

Upvotes: 1

Related Questions