Search code examples
phpregexurlhyperlinkbbcode

Replace [url]text[/url] BBCode placeholders with HTML hyperlinks


I have the following code:

$string = '[url]http://example.com[/url]';
$bbreplace = array('/\[url\](.+?)\[\/url\]/');
$bbreplacements = array('<a href=\"\\1\">\\1</a>');
$string = preg_replace($bbreplace, $bbreplacements, $string);
print $string;

Which creates a URL called http://example.com/ that points to

mydomain.com/"http://example.com/" 

instead of

http://example.com/

How can I fix this?


Solution

  • You don't need to escape the " inside '.

    $bbreplacements = array ('<a href="\\1">\\1</a>');