X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FUtil%2FStrings.php;h=2405fbababcfdbcd8d6788f4ed28040c6b954c4f;hb=3749892f590088275f82d2c90ff1372d24e2d9ec;hp=84a8b6727a3d4d4383bf22a1c06d5b58d2682f24;hpb=08ead524339a43f29c809ca63514e40cd24dee9c;p=friendica.git diff --git a/src/Util/Strings.php b/src/Util/Strings.php index 84a8b6727a..2405fbabab 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', @@ -515,4 +530,35 @@ class Strings return $text; } + + /** + * This function converts a PHP's shorhand notation string for file sizes in 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 + * @return int + */ + public static function getBytesFromShorthand(string $shorthand): int + { + $shorthand = trim($shorthand); + + if (is_numeric($shorthand)) { + return $shorthand; + } + + $last = strtolower($shorthand[strlen($shorthand)-1]); + $shorthand = substr($shorthand, 0, -1); + + switch($last) { + case 'g': + $shorthand *= 1024; + case 'm': + $shorthand *= 1024; + case 'k': + $shorthand *= 1024; + } + + return $shorthand; + } + }