X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=sidebyside;f=src%2FUtil%2FStrings.php;h=2405fbababcfdbcd8d6788f4ed28040c6b954c4f;hb=1d6f5c33a1a7c538d4e529d22701fd735855da15;hp=44ddc732599e81a5bff34195a8c3c33f516b80c7;hpb=d2ca812647c0c06665e008354aa692d492f8857a;p=friendica.git diff --git a/src/Util/Strings.php b/src/Util/Strings.php index 44ddc73259..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 + )*'; } /** * Ensures a single path item doesn't contain any path-traversing characters * - * @see https://stackoverflow.com/a/46097713 * @param string $pathItem + * + * @see https://stackoverflow.com/a/46097713 * @return string */ public static function sanitizeFilePathItem(string $pathItem): string @@ -436,6 +450,7 @@ class Strings * @param string $replacement * @param int $start * @param int|null $length + * * @return string * @see substr_replace() */ @@ -474,6 +489,7 @@ class Strings * @param string $text * @param string $regex * @param callable $callback + * * @return string */ public static function performWithEscapedBlocks(string $text, string $regex, callable $callback): string @@ -483,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) . '»'; @@ -494,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', @@ -510,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; + } + }