diff options
Diffstat (limited to 'it.class')
-rw-r--r-- | it.class | 40 |
1 files changed, 14 insertions, 26 deletions
@@ -165,7 +165,7 @@ function toascii($text) /** - * Convert regex for preg (redefines \w,\W,\b,\B, adds and escapes delimiter, adds modifiers) + * Convert regex for preg (adds and escapes delimiter, adds modifiers) * @param $pattern Regex to convert * @param named parameter casesensitive Regex is case sensitive (omit modifier i) * @param named parameter multiline add modifier m @@ -176,31 +176,8 @@ function toascii($text) */ function convertregex( $pattern, $p = array() ) { - $wordchar = 'a-zA-Z0-9_\xa0-\xff'; - $nonwordchar = '\x00-\x2f\x3a-\x40\x5B-\x60\x7b-\x9f'; - - #matches only even number of backslashes (double escaped for php and preg) - $nonesc = '(?<!\\\\)(?:\\\\\\\\)*'; - - #matches unescaped '[' until unescaped and not starting ']', starting ] as in []gna] or [^]gna] - $incharclass = "$nonesc\\[\\^?\\]?(?:[^]]|$nonesc\\\\])*"; - $pattern = preg_replace( "|/|", '\/', $pattern ); - $replaces = array( - # \w in character class - "/($incharclass$nonesc)\\\\w/" => "\$1$wordchar", - # \W in character class - "/($incharclass$nonesc)\\\\W/" => "\$1$nonwordchar", - # normal \w - "/($nonesc)\\\\w/" => "\$1[$wordchar]", - # normal \W - "/($nonesc)\\\\W/" => "\$1[$nonwordchar]", - # \b (use negative assertions to match at end of string) - "/($nonesc)\\\\b/" => "\$1(?:(?<![$wordchar])(?![$nonwordchar])|(?<![$nonwordchar])(?![$wordchar]))", - # \B (use positive assertions to not match at end of string) - "/($nonesc)\\\\B/" => "\$1(?:(?<=[$wordchar])(?=[$wordchar])|(?<=[$nonwordchar])(?=[$nonwordchar]))", - ); - $pattern = preg_replace( array_keys( $replaces ), array_values( $replaces ), $pattern ); + $modifiers = ''; if( ! $p['casesensitive'] ) $modifiers .= 'i'; foreach( array( @@ -227,10 +204,15 @@ function match( $pattern, $string, $p = array() ) $flags = 0; if( $p['offset_capture'] ) $flags |= PREG_OFFSET_CAPTURE; + $oldlocale = setlocale( LC_CTYPE, 0 ); + if( $oldlocale != 'de_CH' ) + setlocale( LC_CTYPE, 'de_CH' ); if( $p['all'] ) $r = preg_match_all( it::convertregex( $pattern, $p ), $string, $m, $flags | PREG_PATTERN_ORDER, $p['offset'] ); else $r = preg_match( it::convertregex( $pattern, $p ), $string, $m, $flags, $p['offset'] ); + if( $oldlocale != 'de_CH' ) + setlocale( LC_CTYPE, $oldlocale ); # no match if( !$r ) return $p['all'] ? array() : false; @@ -259,7 +241,13 @@ function replace( $replacements, $string, $p = array() ) $patterns = array(); foreach( array_keys( $replacements ) as $pat ) $patterns[] = it::convertregex( $pat, $p ); - return preg_replace( $patterns, array_values( $replacements ), $string, isset( $p['limit'] ) ? $p['limit'] : -1 ); + $oldlocale = setlocale( LC_CTYPE, 0 ); + if( $oldlocale != 'de_CH' ) + setlocale( LC_CTYPE, 'de_CH' ); + $r = preg_replace( $patterns, array_values( $replacements ), $string, isset( $p['limit'] ) ? $p['limit'] : -1 ); + if( $oldlocale != 'de_CH' ) + setlocale( LC_CTYPE, $oldlocale ); + return $r; } } |