[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

/libraries/phputf8/native/ -> core.php (source)

   1  <?php
   2  /**
   3  * @version $Id: core.php 10381 2008-06-01 03:35:53Z pasamio $
   4  * @package utf8
   5  * @subpackage strings
   6  */
   7  
   8  /**
   9  * Define UTF8_CORE as required
  10  */
  11  if ( !defined('UTF8_CORE') ) {
  12      define('UTF8_CORE',TRUE);
  13  }
  14  
  15  //--------------------------------------------------------------------
  16  /**
  17  * UTF-8 aware alternative to strpos
  18  * Find position of first occurrence of a string
  19  * Note: This will get alot slower if offset is used
  20  * Note: requires utf8_strlen amd utf8_substr to be loaded
  21  * @param string haystack
  22  * @param string needle (you should validate this with utf8_is_valid)
  23  * @param integer offset in characters (from left)
  24  * @return mixed integer position or FALSE on failure
  25  * @see http://www.php.net/strpos
  26  * @see utf8_strlen
  27  * @see utf8_substr
  28  * @package utf8
  29  * @subpackage strings
  30  */
  31  function utf8_strpos($str, $needle, $offset = NULL) {
  32  
  33      if ( is_null($offset) ) {
  34  
  35          $ar = explode($needle, $str);
  36          if ( count($ar) > 1 ) {
  37              return utf8_strlen($ar[0]);
  38          }
  39          return FALSE;
  40  
  41      } else {
  42  
  43          if ( !is_int($offset) ) {
  44              trigger_error('utf8_strpos: Offset must be an integer',E_USER_ERROR);
  45              return FALSE;
  46          }
  47  
  48          $str = utf8_substr($str, $offset);
  49  
  50          if ( FALSE !== ( $pos = utf8_strpos($str, $needle) ) ) {
  51              return $pos + $offset;
  52          }
  53  
  54          return FALSE;
  55      }
  56  
  57  }
  58  
  59  //--------------------------------------------------------------------
  60  /**
  61  * UTF-8 aware alternative to strrpos
  62  * Find position of last occurrence of a char in a string
  63  * Note: This will get alot slower if offset is used
  64  * Note: requires utf8_substr and utf8_strlen to be loaded
  65  * @param string haystack
  66  * @param string needle (you should validate this with utf8_is_valid)
  67  * @param integer (optional) offset (from left)
  68  * @return mixed integer position or FALSE on failure
  69  * @see http://www.php.net/strrpos
  70  * @see utf8_substr
  71  * @see utf8_strlen
  72  * @package utf8
  73  * @subpackage strings
  74  */
  75  function utf8_strrpos($str, $needle, $offset = NULL) {
  76  
  77      if ( is_null($offset) ) {
  78  
  79          $ar = explode($needle, $str);
  80  
  81          if ( count($ar) > 1 ) {
  82              // Pop off the end of the string where the last match was made
  83              array_pop($ar);
  84              $str = join($needle,$ar);
  85              return utf8_strlen($str);
  86          }
  87          return FALSE;
  88  
  89      } else {
  90  
  91          if ( !is_int($offset) ) {
  92              trigger_error('utf8_strrpos expects parameter 3 to be long',E_USER_WARNING);
  93              return FALSE;
  94          }
  95  
  96          $str = utf8_substr($str, $offset);
  97  
  98          if ( FALSE !== ( $pos = utf8_strrpos($str, $needle) ) ) {
  99              return $pos + $offset;
 100          }
 101  
 102          return FALSE;
 103      }
 104  
 105  }
 106  
 107  //--------------------------------------------------------------------
 108  /**
 109  * UTF-8 aware alternative to substr
 110  * Return part of a string given character offset (and optionally length)
 111  * Note: supports use of negative offsets and lengths but will be slower
 112  * when doing so
 113  * @param string
 114  * @param integer number of UTF-8 characters offset (from left)
 115  * @param integer (optional) length in UTF-8 characters from offset
 116  * @return mixed string or FALSE if failure
 117  * @package utf8
 118  * @subpackage strings
 119  */
 120  function utf8_substr($str, $offset, $length = NULL) {
 121  
 122      if ( $offset >= 0 && $length >= 0 ) {
 123  
 124          if ( $length === NULL ) {
 125              $length = '*';
 126          } else {
 127              if ( !preg_match('/^[0-9]+$/', $length) ) {
 128                  trigger_error('utf8_substr expects parameter 3 to be long', E_USER_WARNING);
 129                  return FALSE;
 130              }
 131  
 132              $strlen = strlen(utf8_decode($str));
 133              if ( $offset > $strlen ) {
 134                  return '';
 135              }
 136  
 137              if ( ( $offset + $length ) > $strlen ) {
 138                 $length = '*';
 139              } else {
 140                  $length = '{'.$length.'}';
 141              }
 142          }
 143  
 144          if ( !preg_match('/^[0-9]+$/', $offset) ) {
 145              trigger_error('utf8_substr expects parameter 2 to be long', E_USER_WARNING);
 146              return FALSE;
 147          }
 148  
 149          $pattern = '/^.{'.$offset.'}(.'.$length.')/us';
 150  
 151          preg_match($pattern, $str, $matches);
 152  
 153          if ( isset($matches[1]) ) {
 154              return $matches[1];
 155          }
 156  
 157          return FALSE;
 158  
 159      } else {
 160  
 161          // Handle negatives using different, slower technique
 162          // From: http://www.php.net/manual/en/function.substr.php#44838
 163          preg_match_all('/./u', $str, $ar);
 164          if( $length !== NULL ) {
 165              return join('',array_slice($ar[0],$offset,$length));
 166          } else {
 167              return join('',array_slice($ar[0],$offset));
 168          }
 169      }
 170  }


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1