Error creating link dynamically

0

I got the following BBCode code that perfectly fits the logic of what I need to create a link. But it has the following errors:

  

Warning: Unexpected character in input: '\' (ASCII = 92) state = 1 in   /home/p3h8com/public_html/teste.php on line 3

     

Parse error: syntax error, unexpected ';' in   /home/p3h8com/public_html/teste.php on line 3

$str = '[url=http://www.abc.com.br]Blog do Beraldo[/url]';
$str = preg_replace( "/\[url=(.*?)\](.*?)\[\/url\]/i", "<a href="\&quot;$1\&quot;" target="\&quot;blank\&quot;">$2</a>", $str );
echo $str;
    
asked by anonymous 19.09.2018 / 20:39

1 answer

0

You are giving string breaks. You are delimiting the HTML attributes with double quotes and at the same time the PHP string.

"<a href="\&quot;$1\&quot;" target="\&quot;blank\&quot;">$2</a>"
         ↑                ↑        ↑                   ↑

Change the quotation marks of single-quote attributes that resolve, and do not need the \&quot; codes, because it will generate unnecessary quotation marks by invalidating the link:

$str = '[url=http://www.abc.com.br]Blog do Beraldo[/url]';
$str = preg_replace( "/\[url=(.*?)\](.*?)\[\/url\]/i", "<a href='$1' target='_blank'>$2</a>", $str );
echo $str;
    
19.09.2018 / 21:33