php - Regex to select url except when = is directly infront of it -
i'm trying use regex find , replace urls in forum system. works selects within bbcode. shouldn't happening.
my code follows:
<?php function make_links_clickable($text){ return preg_replace('!(([^=](f|ht)tp(s)?://)[-a-za-zа-яА-Я()0-9@:%_+.~#?&;//=]+)!i', '<a href="$1">$1</a>', $text); } //$text = "https://www.mcgamerzone.com<br>http://www.mcgamerzone.com/help/support<br>just text<br>http://www.google.com/<br><b>more text</b>"; $text = "@theareak know and [b][url=https://www.mcgamerzone.com/news/67/false-positive-proxy-bans-and-bot-attacks]here[/url] [/b]is explanation, trying fix asap! https://www.mcgamerzone.com/news/67/false-positive-proxy-bans-and-bot-attacks aaa"; echo "<b>unparsed text:</b><br>"; echo $text; echo "<br><br>"; echo "<b>parsed text:</b><br>"; echo make_links_clickable($text); ?> all urls occur in bb-code following on = character, meaning don't want starts = selected.
i have working results in selecting 1 character in in front of string should selected.
i'm not familiar regex. final output of code this:
<b>unparsed text:</b><br> @theareak know and [b][url=https://www.mcgamerzone.com/news/67/false-positive-proxy-bans-and-bot-attacks]here[/url] [/b]is explanation, trying fix asap! https://www.mcgamerzone.com/news/67/false-positive-proxy-bans-and-bot-attacks aaa<br> <br> <b>parsed text:</b><br> @theareak know and [b][url=https://www.mcgamerzone.com/news/67/false-positive-proxy-bans-and-bot-attacks]here[/url] [/b]is explanation, trying fix asap!<a href=" https://www.mcgamerzone.com/news/67/false-positive-proxy-bans-and-bot-attacks"> https://www.mcgamerzone.com/news/67/false-positive-proxy-bans-and-bot-attacks</a> aaa
you can match , skip [url=...] this:
\[url=[^\]]*](*skip)(?!)|(((f|ht)tps?://)[-a-za-zа-яёЁА-Я()0-9@:%_+.\~#?&;/=]+) see regex demo
that way, match urls outside [url=...] tag.
function make_links_clickable($text){ return preg_replace('~\[url=[^\]]*](*skip)(?!)|(((f|ht)tps?://)[-a-za-zа-яёЁА-Я()0-9@:%_+.\~#?&;/=]+)~iu', '<a href="$1">$1</a>', $text); } $text = "@theareak know , [b][url=https://www.mcgamerzone.com/news/67/false-positive-proxy-bans-and-bot-attacks]here[/url] [/b]is explanation, trying fix asap! https://www.mcgamerzone.com/news/67/false-positive-proxy-bans-and-bot-attacks aaa"; echo "<b>parsed text:</b><br>"; echo make_links_clickable($text); 
Comments
Post a Comment