diff options
-rw-r--r-- | it.class | 106 |
1 files changed, 60 insertions, 46 deletions
@@ -174,21 +174,26 @@ function toascii($text) * @param named parameter extended add modifier x * @return converted regex to use with preg */ -function convertregex( $pattern, $p = array() ) +function convertregex($pattern, $p = array()) { - $pattern = preg_replace( "|/|", '\/', $pattern ); + $pattern = preg_replace('|/|', '\/', $pattern); $modifiers = ''; - if( ! $p['casesensitive'] ) + + if (!$p['casesensitive']) $modifiers .= 'i'; - foreach( array( - 'multiline' => 'm', - 'singleline' => 's', - 'utf8' => 'u', - 'extended' => 'x', - ) as $key => $mod ) - if( $p[$key] ) - $moifiers .= $mod; - return "/" . $pattern . "/" . $modifiers; + + foreach (array( + 'multiline' => 'm', + 'singleline' => 's', + 'utf8' => 'u', + 'extended' => 'x', + ) as $key => $mod) + { + if ($p[$key]) + $modifiers .= $mod; + } + + return "/$pattern/$modifiers"; } /** @@ -199,35 +204,38 @@ function convertregex( $pattern, $p = array() ) * @param named parameter all Return every match as array instead of first match. * @return Matched string or false */ -function match( $pattern, $string, $p = array() ) +function match($pattern, $string, $p = array()) { $flags = 0; - if( $p['offset_capture'] ) + + 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'] ); + + 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; - # no capture - else if( count( $m ) == 1 ) - return $m[0]; - # one capture - else if( count( $m ) == 2 ) - return $m[1]; - # captures, reorder pattern_order to set_order but without first element - else if( $p['all'] && !$p['pattern_order'] ) - return call_user_func_array( 'array_map', array_merge( array( null ), array_slice( $m, 1 ) ) ); - # captures, don't return first element (matched string) - else - return array_slice( $m, 1 ); + $r = preg_match(it::convertregex($pattern, $p), $string, $m, $flags, $p['offset']); + + if ($oldlocale != 'de_CH') + setlocale(LC_CTYPE, $oldlocale); + + if (!$r) # no match + $result = $p['all'] ? array() : false; + else if (count($m) == 1) # no capture + $result = $m[0]; + else if (count($m) == 2) # one capture + $result = $m[1]; + else if ($p['all'] && !$p['pattern_order']) # captures, reorder pattern_order to set_order but without first element + $result = call_user_func_array('array_map', array_merge(array(null), array_slice($m, 1))); + else # captures, don't return first element (matched string) + $result = array_slice($m, 1); + + return $result; } /** @@ -236,18 +244,24 @@ function match( $pattern, $string, $p = array() ) * @param $string String to change. * @return New string. */ -function replace( $replacements, $string, $p = array() ) +function replace($replacements, $string, $p = array()) { $patterns = array(); - foreach( array_keys( $replacements ) as $pat ) - $patterns[] = it::convertregex( $pat, $p ); - $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; + + foreach (array_keys( $replacements ) as $pat) + $patterns[] = it::convertregex($pat, $p); + + $oldlocale = setlocale(LC_CTYPE, 0); + + if ($oldlocale != 'de_CH') + setlocale(LC_CTYPE, 'de_CH'); + + $result = preg_replace($patterns, array_values($replacements), $string, isset($p['limit']) ? $p['limit'] : -1); + + if ($oldlocale != 'de_CH') + setlocale(LC_CTYPE, $oldlocale); + + return $result; } } |