Error creating link dynamically

Asked

Viewed 20 times

0

I got the following Bbcode code that perfectly fits the logic of what I need to create link. Only it presents 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;

1 answer

0

You are breaking the string. 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 attribute quotes with simple quotes that solves, and you don’t need codes \&quot;, because it will generate unnecessary quotes 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;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.