From 379f4d18c3389ccc07bab9d0a35518092c10ff24 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 8 Mar 2019 23:39:21 -0500 Subject: [PATCH] Update autolinker regular expression in Text\BBCode - Thanks to https://daringfireball.net/2010/07/improved_regex_for_matching_urls --- src/Content/Text/BBCode.php | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index e49b14f47c..85466b50f6 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -1268,11 +1268,29 @@ class BBCode extends BaseObject // if the HTML is used to generate plain text, then don't do this search, but replace all URL of that kind to text if (!$for_plaintext) { - // Autolink feature (thanks to http://code.seebz.net/p/autolink-php/) - // Currently disabled, since the function is too greedy - // $autolink_regex = "`([^\]\=\"']|^)(https?\://[^\s<]+[^\s<\.\)])`ism"; - $autolink_regex = "/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism"; - $text = preg_replace($autolink_regex, '$1[url]$2[/url]', $text); + // Autolink feature (thanks to https://daringfireball.net/2010/07/improved_regex_for_matching_urls) + $autolink_regex = '@(?xi) +(?]+ # Run of non-space, non-()<> + | # or + \(([^\s()<>]+|(\([^\s()<>]+\)))*\) # balanced parens, up to 2 levels + )+ + (?: # End with: + \(([^\s()<>]+|(\([^\s()<>]+\)))*\) # balanced parens, up to 2 levels + | # or + [^\s`!()\[\]{};:\'".,<>?«»“”‘’] # not a space or one of these punct chars + ) +)@'; + $text = preg_replace($autolink_regex, '[url]$1[/url]', $text); if ($simple_html == 7) { $text = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism", 'self::convertUrlForOStatusCallback', $text); $text = preg_replace_callback("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", 'self::convertUrlForOStatusCallback', $text); -- 2.39.5