Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: utf8

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /phputf8/native/core.php

Documentation is available at core.php

  1. <?php
  2. /**
  3. @version $Id: core.php,v 1.4 2006/02/25 13:54:31 harryf Exp $
  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{
  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{
  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 >= && $length >= {
  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. }

Documentation generated on Mon, 05 Mar 2007 20:55:50 +0000 by phpDocumentor 1.3.1