X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FUtil%2FStrings.php;h=bc1a0e4be991e751634c016ae1c5177ea06377bd;hb=0a45bdd3b71ec0f8744abb3830858a91a4bca146;hp=0972e44486afc811236bb572d4360689db824d93;hpb=765b50bde8cd2cb9e4f89fd67d81c1c1509b5261;p=friendica.git diff --git a/src/Util/Strings.php b/src/Util/Strings.php index 0972e44486..bc1a0e4be9 100644 --- a/src/Util/Strings.php +++ b/src/Util/Strings.php @@ -1,6 +1,6 @@ ?«»“”‘’.] # Domain can\'t start with a . - [^/\s\xA0`!()\[\]{};:\'",<>?«»“”‘’]+ # Domain can\'t end with a . + [^/\s\xA0`!()\[\]{};:\'",<>?«»“”‘’.] # Domain can\'t start with a . + [^/\s\xA0`!()\[\]{};:\'",<>?«»“”‘’]+ # Domain can\'t end with a . \. [^/\s\xA0`!()\[\]{};:\'".,<>?«»“”‘’]+/? # Followed by a slash ) - (?: # One or more: - [^\s\xA0()<>]+ # Run of non-space, non-()<> - | # or - \(([^\s\xA0()<>]+|(\([^\s()<>]+\)))*\) # balanced parens, up to 2 levels - | # or - [^\s\xA0`!()\[\]{};:\'".,<>?«»“”‘’] # not a space or one of these punct chars - )* -)@xiu'; + (?: # One or more: + [^\s\xA0()<>]+ # Run of non-space, non-()<> + | # or + \(([^\s\xA0()<>]+|(\([^\s()<>]+\)))*\) # balanced parens, up to 2 levels + | # or + [^\s\xA0`!()\[\]{};:\'".,<>?«»“”‘’] # not a space or one of these punct chars + )*'; } /** @@ -488,7 +499,7 @@ class Strings $blocks = []; - $text = preg_replace_callback($regex, + $return = preg_replace_callback($regex, function ($matches) use ($executionId, &$blocks) { $return = '«block-' . $executionId . '-' . count($blocks) . '»'; @@ -499,7 +510,11 @@ class Strings $text ); - $text = $callback($text) ?? ''; + if (is_null($return)) { + Logger::notice('Received null value from preg_replace_callback', ['text' => $text, 'regex' => $regex, 'blocks' => $blocks, 'executionId' => $executionId, 'callstack' => System::callstack(10)]); + } + + $text = $callback($return ?? $text) ?? ''; // Restore code blocks $text = preg_replace_callback('/«block-' . $executionId . '-([0-9]+)»/iU', @@ -517,7 +532,7 @@ class Strings } /** - * This function converts a PHP's shorhand notation string for file sizes in to an integer number of total bytes. + * This function converts a file size string written in PHP's shorthand notation to an integer number of total bytes. * For example: The string for shorthand notation of '2M' (which is 2,097,152 Bytes) is converted to 2097152 * @see https://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes * @param string $shorthand @@ -546,4 +561,22 @@ class Strings return $shorthand; } + /** + * Converts an URL in a nicer format (without the scheme and possibly shortened) + * + * @param string $url URL that is about to be reformatted + * @return string reformatted link + */ + public static function getStyledURL(string $url): string + { + $parts = parse_url($url); + $scheme = [$parts['scheme'] . '://www.', $parts['scheme'] . '://']; + $styled_url = str_replace($scheme, '', $url); + + if (strlen($styled_url) > 30) { + $styled_url = substr($styled_url, 0, 30) . "…"; + } + + return $styled_url; + } }