From 141a45d12100fd5b374659d4b4f1b464b915e04d Mon Sep 17 00:00:00 2001 From: Nathan Gass Date: Mon, 22 Jan 2007 17:14:47 +0000 Subject: use locale setting for regexes, but revert after preg call --- it.class | 40 ++++++++++++++-------------------------- 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/it.class b/it.class index 2fe2682..26b2055 100644 --- a/it.class +++ b/it.class @@ -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 = '(? "\$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(?:(? "\$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; } } -- cgit v1.2.3