X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Flibs%2FStringUtils.php;h=19dd8fe4d3f98d54921847402854838468d05c06;hb=c14571ba26edbe26606127fdf89cd9793a7488ab;hp=d91ac85adb1a56c99748f9a55a2400aba7a36b78;hpb=1e680456b4b2f5f2a33df4acceafc03d6c8f75b1;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/libs/StringUtils.php b/includes/libs/StringUtils.php index d91ac85adb..19dd8fe4d3 100644 --- a/includes/libs/StringUtils.php +++ b/includes/libs/StringUtils.php @@ -1,4 +1,7 @@ cb(), $subject, $flags ); + return self::delimiterReplaceCallback( + $startDelim, $endDelim, + function ( array $matches ) use ( $replace ) { + return strtr( $replace, [ '$0' => $matches[0], '$1' => $matches[1] ] ); + }, + $subject, $flags + ); } /** @@ -263,8 +269,13 @@ class StringUtils { $text = str_replace( $placeholder, '', $text ); // Replace instances of the separator inside HTML-like tags with the placeholder - $replacer = new DoubleReplacer( $separator, $placeholder ); - $cleaned = self::delimiterReplaceCallback( '<', '>', $replacer->cb(), $text ); + $cleaned = self::delimiterReplaceCallback( + '<', '>', + function ( array $matches ) use ( $separator, $placeholder ) { + return str_replace( $separator, $placeholder, $matches[0] ); + }, + $text + ); // Explode, then put the replaced separators back in $items = explode( $separator, $cleaned ); @@ -290,8 +301,13 @@ class StringUtils { $text = str_replace( $placeholder, '', $text ); // Replace instances of the separator inside HTML-like tags with the placeholder - $replacer = new DoubleReplacer( $search, $placeholder ); - $cleaned = self::delimiterReplaceCallback( '<', '>', $replacer->cb(), $text ); + $cleaned = self::delimiterReplaceCallback( + '<', '>', + function ( array $matches ) use ( $search, $placeholder ) { + return str_replace( $search, $placeholder, $matches[0] ); + }, + $text + ); // Explode, then put the replaced separators back in $cleaned = str_replace( $search, $replace, $cleaned ); @@ -327,4 +343,21 @@ class StringUtils { return new ArrayIterator( explode( $separator, $subject ) ); } } + + /** + * Utility function to check if the given string is a valid regex. Avoids + * manually calling suppressWarnings and restoreWarnings, and provides a + * one-line solution without the need to use @. + * + * @since 1.34 + * @param string $string The string you want to check being a valid regex + * @return bool + */ + public static function isValidRegex( $string ) { + AtEase::suppressWarnings(); + // @phan-suppress-next-line PhanParamSuspiciousOrder False positive + $isValid = preg_match( $string, '' ); + AtEase::restoreWarnings(); + return $isValid !== false; + } }