summaryrefslogtreecommitdiff
path: root/it.class
diff options
context:
space:
mode:
authorNathan Gass2007-01-22 17:14:47 +0000
committerNathan Gass2007-01-22 17:14:47 +0000
commit141a45d12100fd5b374659d4b4f1b464b915e04d (patch)
tree15d716445c5e91ab23a58277eabee2785ac81f81 /it.class
parent6558c11c8b76febea576719196df008fec24ff5e (diff)
downloaditools-141a45d12100fd5b374659d4b4f1b464b915e04d.tar.gz
itools-141a45d12100fd5b374659d4b4f1b464b915e04d.tar.bz2
itools-141a45d12100fd5b374659d4b4f1b464b915e04d.zip
use locale setting for regexes, but revert after preg call
Diffstat (limited to 'it.class')
-rw-r--r--it.class40
1 files 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 = '(?<!\\\\)(?:\\\\\\\\)*';
-
- #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;
}
}