php - URL-BBCode Regex -
i'm trying url bbcode. there 4 possibilities it, , that's current problem:
[url]http://stackoverflow.com/[/url] [url='http://stackoverflow.com/']http://stackoverflow.com/[/url] [url="http://stackoverflow.com/"]http://stackoverflow.com/[/url] [url=http://stackoverflow.com/]http://stackoverflow.com/[/url]
i need both. is, have far:
/\[url(?:\=\'([^\'"]+)\')?](.+?)\[\/url]/i
but work case 1 & 2.
edit:
this works better:
/\[url(?:\=(?:[\"|'])?(.*)(?:[^[]+)?)?\](.*)\[\/url\]/i
but still not perfect.
edit:
i guess, got it. may need optimization, seems work:
/\[url(?:\=("|\'|)?(.*)?\1)?\](.*)\[\/url\]/i
see regexp-tester
you can use pattern:
$pattern = '~\[url(?|=[\'"]?([^]"\']+)[\'"]?]([^[]+)|](([^[]+)))\[/url]~'; $replacement = '<a href="$1">$2</a>'; $result = preg_replace($pattern, $replacement, $subject);
the idea preserve groups numbers each branch of alternation using branch reset feature. in way, group 1 contains url , group 2 link description. when there no description, url used in place, that's why url twice enclosed in capture group second branch.
Comments
Post a Comment