3v4l.org

run code in 300+ PHP versions simultaneously
<?php $bbcodes = [ 'Look at this:https://www.example.com/example?ohyeah=sure#okay this is a raw link', 'No attibute bbcode url: [url]http://example.com/x1[/url]', 'A url with link and link text: [url=http://example.com/x2]x2[/url]', 'Image with "ignorable" text: [IMG=sumpthing.jpg]sumpthing[/IMG]', 'Image: [img=sumpinelse][/img]' ]; $search = array ( // ... '~\[url=((?:ht|f)tps?://[a-z\d.-]+\.[a-z]{2,3}/\S*?)](.*?)\[/url]~i', '~\[url]((?:ht|f)tps?://[a-z\d.-]+\.[a-z]{2,3}/\S*?)\[/url]~i', // ... '~\[img=(.*?)].*?\[/img]~i', // if you want the possibility of dot matching newlines, add s pattern modifier // ... '~(?:<a.*?</a>|<img.*?</img>)(*SKIP)(*FAIL)|\bhttps?://.+?(?=\s|$)~im' // mop up any remaining links that are not bbtagged ); $replace = array ( // ... '<a href="$1" target="_blank">$2</a>', '<a href="$1" target="_blank">$1</a>', // ... '<img src="$1"></img>', // ... '<a href="$0" target="_blank">$0</a>', ); var_export(preg_replace($search, $replace, $bbcodes));

preferences:
38.72 ms | 405 KiB | 5 Q