1

I have a simple questions. I have this function using php laravel that outputs a clickable link. This is part of the back end.

  public function linkify($text){
  //$text = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[A-Z0-9+&@#\/%=~_|]/i', '<a href="\0">\0</a>', $text);
   $text = '<a href="http://google.com">http://google.com</a> ';
      return $text; 
  }

 foreach ($res as $row) {
   $subject= $this->linkify($row['subject']);
   $json['amps'][$x]['subject']= $subject;
   $x++;
  }
 echo json_encode($json);

And this is the front end

{{#each amps}}
        <li class="message">
        <img src="/images/people/{{pic}}" class="chatpic" alt="">
            <div class="message-text">
                {{subject}}         
            </div>
        </li>
    {{/each}}

The problem is that instead of outputting a clickable link it just displays plain text. I also noticed that the link is wrapped with double quotes Here is the source code from the console window

enter image description here

Any help would be appreciated

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Israel Gonzales
  • 19
  • 1
  • 2
  • 7
  • The string will auto escape when you use those. Likely duplicate of [this question](http://stackoverflow.com/questions/29253979/laravel-5-display-html-with-blade). – forrestmid Feb 09 '17 at 00:59

2 Answers2

3

You need to use the proper bracket syntax to show unescaped HTML

<div class="message-text">
            {!! subject !!}         
 </div>

The standard brackets wrap your value in the htmlentities() method behind the scenes to sanitize user input.

Rob Fonseca
  • 3,671
  • 2
  • 17
  • 13
  • I am currently using laravel 4.1.24, which does not support the syntax. Is there an alternative syntax? or do you know if laravel > 5 is backward compatible? – Israel Gonzales Feb 09 '17 at 16:13
  • You will want to create a blade macro. Instruction are found here http://stackoverflow.com/questions/16613110/laravel-4-how-insert-raw-html-to-label – Rob Fonseca Feb 09 '17 at 16:19
  • 1
    I managed to solve this by using {{{ subject }}} instead. Thanks for the help though. I appreciated – Israel Gonzales Feb 09 '17 at 16:55
0

Rather than doing {!! subject !!} use triple brackets instead {{{ subject }}}

Disclaimer : I'm writing Israel Gonzales's comment as an answer, as that's the first link I got from Google for my question and the accepted answer didn't resolve it for me, but his comment did.

Alexandre Elshobokshy
  • 10,720
  • 6
  • 27
  • 57