Example #1
0
 /**
  * @covers Symfony\Polyfill\Iconv\Iconv::iconv_strpos
  * @covers Symfony\Polyfill\Iconv\Iconv::iconv_strrpos
  */
 public function testIconvStrPos()
 {
     $this->assertSame(1, iconv_strpos('11--', '1-', 0, 'UTF-8'));
     $this->assertSame(2, iconv_strpos('-11--', '1-', 0, 'UTF-8'));
     $this->assertSame(false, iconv_strrpos('한국어', '', 'UTF-8'));
     $this->assertSame(1, iconv_strrpos('한국어', '국', 'UTF-8'));
 }
 function _strrpos($str, $search, $offset = null)
 {
     if (is_null($offset)) {
         $old_enc = $this->_setUTF8IconvEncoding();
         $result = iconv_strrpos($str, $search);
         $this->_setIconvEncoding($old_enc);
         return $result;
     } else {
         //mb_strrpos doesn't support offset! :(
         return parent::_strrpos($str, $search, (int) $offset);
     }
 }
Example #3
0
 /**
  * Word wrap
  *
  * @param  string  $string
  * @param  integer $width
  * @param  string  $break
  * @param  boolean $cut
  * @param  string  $charset
  * @return string
  */
 public static function wordWrap($string, $width = 75, $break = "\n", $cut = false, $charset = 'UTF-8')
 {
     $result = array();
     while (($stringLength = iconv_strlen($string, $charset)) > 0) {
         $subString = iconv_substr($string, 0, $width, $charset);
         if ($subString === $string) {
             $cutLength = null;
         } else {
             $nextChar = iconv_substr($string, $width, 1, $charset);
             if ($nextChar === ' ' || $nextChar === $break) {
                 $afterNextChar = iconv_substr($string, $width + 1, 1, $charset);
                 if ($afterNextChar === false) {
                     $subString .= $nextChar;
                 }
                 $cutLength = iconv_strlen($subString, $charset) + 1;
             } else {
                 $spacePos = iconv_strrpos($subString, ' ', $charset);
                 if ($spacePos !== false) {
                     $subString = iconv_substr($subString, 0, $spacePos, $charset);
                     $cutLength = $spacePos + 1;
                 } else {
                     if ($cut === false) {
                         $spacePos = iconv_strpos($string, ' ', 0, $charset);
                         if ($spacePos !== false) {
                             $subString = iconv_substr($string, 0, $spacePos, $charset);
                             $cutLength = $spacePos + 1;
                         } else {
                             $subString = $string;
                             $cutLength = null;
                         }
                     } else {
                         $breakPos = iconv_strpos($subString, $break, 0, $charset);
                         if ($breakPos !== false) {
                             $subString = iconv_substr($subString, 0, $breakPos, $charset);
                             $cutLength = $breakPos + 1;
                         } else {
                             $subString = iconv_substr($subString, 0, $width, $charset);
                             $cutLength = $width;
                         }
                     }
                 }
             }
         }
         $result[] = $subString;
         if ($cutLength !== null) {
             $string = iconv_substr($string, $cutLength, $stringLength - $cutLength, $charset);
         } else {
             break;
         }
     }
     return implode($break, $result);
 }
Example #4
0
function foo($haystk, $needle, $to_charset = false, $from_charset = false)
{
    if ($from_charset !== false) {
        $haystk = iconv($from_charset, $to_charset, $haystk);
    }
    if ($to_charset !== false) {
        var_dump(iconv_strlen($haystk, $to_charset));
        var_dump(iconv_strrpos($haystk, $needle, $to_charset));
    } else {
        var_dump(iconv_strlen($haystk));
        var_dump(iconv_strrpos($haystk, $needle));
    }
}
Example #5
0
 /**
  * Here used as a multibyte enabled equivalent of `strrpos()`.
  *
  * @link http://php.net/function.iconv-strpos.php
  * @param string $haystack
  * @param string $needle
  * @return integer|boolean
  */
 public function strrpos($haystack, $needle)
 {
     return iconv_strrpos($haystack, $needle, 'UTF-8');
 }
Example #6
0
function dle_strrpos($str, $needle, $charset)
{
    if (strtolower($charset) == "utf-8") {
        return iconv_strrpos($str, $needle, "utf-8");
    } else {
        return strrpos($str, $needle);
    }
}
 /**
  * Find position of last occurrence of a char in a string, both arguments are in UTF-8.
  *
  * @param string $haystack UTF-8 string to search in
  * @param string $needle UTF-8 character to search for (single character)
  * @return int The character position
  * @see strrpos()
  */
 public function utf8_strrpos($haystack, $needle)
 {
     if ($this->getConversionStrategy() === self::STRATEGY_MBSTRING) {
         return mb_strrpos($haystack, $needle, 'utf-8');
     } elseif ($this->getConversionStrategy() === self::STRATEGY_ICONV) {
         return iconv_strrpos($haystack, $needle, 'utf-8');
     }
     $byte_pos = strrpos($haystack, $needle);
     if ($byte_pos === false) {
         // Needle not found
         return false;
     }
     return $this->utf8_byte2char_pos($haystack, $byte_pos);
 }
Example #8
0
 /**
  * Find position of last occurrence of a char in a string, both arguments are in UTF-8.
  *
  * @param string $haystack UTF-8 string to search in
  * @param string $needle UTF-8 character to search for (single character)
  * @return int The character position
  * @see strrpos()
  */
 public function utf8_strrpos($haystack, $needle)
 {
     if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['t3lib_cs_utils'] === 'mbstring') {
         return mb_strrpos($haystack, $needle, 'utf-8');
     } elseif ($GLOBALS['TYPO3_CONF_VARS']['SYS']['t3lib_cs_utils'] === 'iconv') {
         return iconv_strrpos($haystack, $needle, 'utf-8');
     }
     $byte_pos = strrpos($haystack, $needle);
     if ($byte_pos === FALSE) {
         // Needle not found
         return FALSE;
     }
     return $this->utf8_byte2char_pos($haystack, $byte_pos);
 }
//get an unset variable
$unset_var = 10;
unset($unset_var);
// get a class
class classA
{
    public function __toString()
    {
        return "world";
    }
}
// heredoc string
$heredoc = <<<EOT
world
EOT;
// get a resource variable
$fp = fopen(__FILE__, "r");
// unexpected values to be passed to $needle argument
$inputs = array(0, 1, 12345, -2345, 10.5, -10.5, 123456789000.0, 1.23456789E-9, 0.5, NULL, null, true, false, TRUE, FALSE, "", '', "world", 'world', $heredoc, new classA(), @$undefined_var, @$unset_var, $fp);
// loop through each element of $inputs to check the behavior of iconv_strrpos()
$iterator = 1;
foreach ($inputs as $input) {
    echo "\n-- Iteration {$iterator} --\n";
    var_dump(iconv_strrpos($haystack, $input, $encoding));
    $iterator++;
}
fclose($fp);
echo "Done";
?>

Example #10
0
 protected static function grapheme_position($s, $needle, $offset, $mode)
 {
     if ($offset > 0) {
         $s = (string) self::grapheme_substr($s, $offset);
     } else {
         if ($offset < 0) {
             $offset = 0;
         }
     }
     if ('' === (string) $needle) {
         return false;
     }
     if ('' === (string) $s) {
         return false;
     }
     switch ($mode) {
         case 0:
             $needle = iconv_strpos($s, $needle, 0, 'UTF-8');
             break;
         case 1:
             $needle = mb_stripos($s, $needle, 0, 'UTF-8');
             break;
         case 2:
             $needle = iconv_strrpos($s, $needle, 'UTF-8');
             break;
         default:
             $needle = mb_strripos($s, $needle, 0, 'UTF-8');
             break;
     }
     return $needle ? self::grapheme_strlen(iconv_substr($s, 0, $needle, 'UTF-8')) + $offset : $needle;
 }
Example #11
0
    function search()
    {
        if ($this->status) {
            $q = "SELECT * FROM sum_base WHERE userid = " . $this->sum_user['id'] . " ORDER BY `date` DESC";
            ?>
		 <script> 
		  var Base64={_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:function(e){var t="";var n,r,i,s,o,u,a;var f=0;e=Base64._utf8_encode(e);while(f<e.length){n=e.charCodeAt(f++);r=e.charCodeAt(f++);i=e.charCodeAt(f++);s=n>>2;o=(n&3)<<4|r>>4;u=(r&15)<<2|i>>6;a=i&63;if(isNaN(r)){u=a=64}else if(isNaN(i)){a=64}t=t+this._keyStr.charAt(s)+this._keyStr.charAt(o)+this._keyStr.charAt(u)+this._keyStr.charAt(a)}return t},decode:function(e){var t="";var n,r,i;var s,o,u,a;var f=0;e=e.replace(/[^A-Za-z0-9\+\/\=]/g,"");while(f<e.length){s=this._keyStr.indexOf(e.charAt(f++));o=this._keyStr.indexOf(e.charAt(f++));u=this._keyStr.indexOf(e.charAt(f++));a=this._keyStr.indexOf(e.charAt(f++));n=s<<2|o>>4;r=(o&15)<<4|u>>2;i=(u&3)<<6|a;t=t+String.fromCharCode(n);if(u!=64){t=t+String.fromCharCode(r)}if(a!=64){t=t+String.fromCharCode(i)}}t=Base64._utf8_decode(t);return t},_utf8_encode:function(e){e=e.replace(/\r\n/g,"\n");var t="";for(var n=0;n<e.length;n++){var r=e.charCodeAt(n);if(r<128){t+=String.fromCharCode(r)}else if(r>127&&r<2048){t+=String.fromCharCode(r>>6|192);t+=String.fromCharCode(r&63|128)}else{t+=String.fromCharCode(r>>12|224);t+=String.fromCharCode(r>>6&63|128);t+=String.fromCharCode(r&63|128)}}return t},_utf8_decode:function(e){var t="";var n=0;var r=c1=c2=0;while(n<e.length){r=e.charCodeAt(n);if(r<128){t+=String.fromCharCode(r);n++}else if(r>191&&r<224){c2=e.charCodeAt(n+1);t+=String.fromCharCode((r&31)<<6|c2&63);n+=2}else{c2=e.charCodeAt(n+1);c3=e.charCodeAt(n+2);t+=String.fromCharCode((r&15)<<12|(c2&63)<<6|c3&63);n+=3}}return t}}
		  function actpay(x){
		    $.jAlert({
			 'title': 'Проверьте данные:',
		     'content': Base64.decode(x),
		     'theme': 'green',
			 'showAnimation': 'fadeInUp'
			});	
		  }
		  function pay(y){
		    $.jAlert({
			 'title': 'Скачать Д/К:',
		     'content': '<span class="filter"><a class="c3" target="_blank" href="test2.php?id='+y+'">Д/К (21 знак)</a>&nbsp;&nbsp;&nbsp;&nbsp;<a class="c1" target="_blank" href="test3.php?id='+y+'">Д/К (15 знаков)</a></span>',
		     'theme': 'green',
			 'showAnimation': 'fadeInUp'
			});	
		  }		  
 		  function delit(z){
		    $.jAlert({
			 'title': 'Удалить Д/К:',
		     'content': '<center>Вы действительно хотите удалить Д/К? </center><div class="ja_btn_wrap optBack"><a href="?delete&id='+z+'" class="ja_btn  ja_btn_default">Удалить</a> </div>',
		     'theme': 'green',
			 'showAnimation': 'fadeInUp'
			});	
		  }				   
		 </script>
		<?php 
            $res = mysql_query($q);
            echo '<br>
		   <div style="width: 90%; margin: auto; vertical-align: top;">
		    <div style="width: 40%; float: left; vertical-align: top; margin-top: -5px; margin-bottom: 10px;" class="filter">
			 <a class="btn btn-warning active" href="?filter&from=' . strtotime(date('Y-m-01')) . '&to=' . strtotime(date('Y-m-15')) . '">1-15 числа</a>
			 <a class="btn btn-warning active" href="?filter&from=' . strtotime(date('Y-m-15')) . '&to=' . strtotime(date('Y-m-31')) . '">15-31 числа</a>
			 <a class="btn btn-warning active" href="?myorder">Все заявки</a>
			</div>
		    <div style="width: 50%; float: right; text-align: right; vertical-align: top;">
		     <form method="post" class="qwa" action="?search"><input type="text" required="" name="key"><button class="registration_button btn btn-success najti">Найти</button></form>
			</div></div><br>';
            echo '<br><table class="owned" width="90%" style="margin: auto;">';
            echo '<tr>
		  <td><b>№</b></td>
		  <td><b><i class="glyphicon glyphicon-user"></i> ФИО заявителя</b></td>
		  <td><b><i class="glyphicon glyphicon-list-alt"></i> Транспортное средство</b></td>
		  <td><b><i class="glyphicon glyphicon-barcode"></i> Номер кузова</b></td>
		  <td><b><i class="glyphicon glyphicon-calendar"></i> Год выпуска</b></td>
		  <td><b><i class="glyphicon glyphicon-barcode"></i> Код ЕАИСТО</b></td>
		  <td><b><i class="glyphicon glyphicon-time"></i> Срок</b></td>
		  <td><b><i class="glyphicon glyphicon-shopping-cart"></i> Стоимость</b></td>
		  <td><b><i class="glyphicon glyphicon-info-sign"></i> Статус</b></td>
		  <td><b><i class="glyphicon glyphicon-calendar"></i> Дата</b></td>
		  <td><b>Операции</b></td>
		 </tr>';
            while ($row = mysql_fetch_array($res)) {
                $allstring = $row['field1'] . ' ' . $row['field2'] . ' ' . $row['field3'] . ' ' . $row['field6'] . ' ' . $row['field7'] . ' ' . $row['field5'] . ' ' . $row['field11'] . ' ' . $row['longg'] . ' ' . $row['price'];
                $allstring = mb_strtoupper($allstring, 'UTF-8');
                if (iconv_strrpos($allstring, mb_strtoupper($_POST['key'], 'UTF-8'), 'UTF-8')) {
                    $qqq = 'true';
                } else {
                    $qqq = 'false';
                }
                if ($qqq == 'true') {
                    $tormoz = str_replace("Hydraulic", "Гидравлический", $row['field16']);
                    $tormoz = str_replace("Pneumatic", "Пневматический", $tormoz);
                    $tormoz = str_replace("Mechanical", "Механический", $tormoz);
                    $tormoz = str_replace("Combined", "Комбинированный", $tormoz);
                    $tormoz = str_replace("None", "Отстутствует", $tormoz);
                    $petrol = str_replace("Petrol", "Бензин", $row['field17']);
                    $petrol = str_replace("Diesel", "Дизельное топливо", $petrol);
                    $petrol = str_replace("PressureGas", "Сжатый газ", $petrol);
                    $petrol = str_replace("LiquefiedGas", "Сжиженный газ", $petrol);
                    $petrol = str_replace("None", "Без топлива", $petrol);
                    $formed = '
		  <b>ФИО:</b> ' . $row['field1'] . ' ' . $row['field2'] . ' ' . $row['field3'] . '<br>
		  <b>Гос. номер:</b> ' . $row['field4'] . '<br>
		  <b>VIN:</b> ' . $row['field5'] . '<br>
		  <b>Авто:</b> ' . $row['field6'] . ' ' . $row['field7'] . '<br>
		  <b>Год выпуска:</b> ' . $row['field11'] . '<br>
		  <b>Шасси/рама:</b> ' . $row['field12'] . '<br>
		  <b>Кузов:</b> ' . $row['field13'] . '<br>
		  <b>Масса / (макс.):</b> ' . $row['field15'] . ' / ' . $row['field14'] . '<br>
		  <b>Тормозная система:</b> ' . $tormoz . '<br>
		  <b>Топливо:</b> ' . $petrol . '<br>
		  <b>Пробег:</b> ' . $row['field18'] . '<br>
		  <b>Шины:</b> ' . $row['field19'] . '<br>
		  <b>Серия, номер документа:</b> ' . $row['field23'] . ' ' . $row['field24'] . '<br>
		  <b>Кем выдан:</b> ' . $row['field26'] . '<br>
		  <b>Цена:</b> ' . $row['price'] . '
		  <div class="ja_btn_wrap optBack"><a class="ja_btn  ja_btn_default" href="?payit&id=' . $row['id'] . '">Оплатить</a> </div>
		  ';
                    $formed = "'" . base64_encode($formed) . "'";
                    if ($row['payeed'] == '1') {
                        $ispayeed = 'Оплачено';
                        $linkpay = '
		     <a onclick="pay(' . $row['id'] . '); return false;" href="?iedown&forieid=' . $row['id'] . '" class="btn btn-primary btn-sm save_pdf"><span class="glyphicon  glyphicon-download-alt"></span>&nbsp;Скачать Д/К</a>';
                    } else {
                        $ispayeed = 'Не оплачено';
                        $linkpay = '
			 <a onclick="actpay(' . $formed . '); return false;" href="?payit&id=' . $row['id'] . '" class="btn btn-primary btn-sm save_pdf"><i class="glyphicon glyphicon-shopping-cart"></i>&nbsp;Оплатить</a>
			 ';
                    }
                    echo '<tr>';
                    echo '<td>&nbsp;' . str_repeat("0", 7 - strlen($row['id'])) . $row['id'] . '&nbsp;</td>';
                    echo '<td>' . $row['field1'] . ' ' . $row['field2'] . ' ' . $row['field3'] . '</td>';
                    echo '<td>' . $row['field6'] . ' ' . $row['field7'] . '</td>';
                    echo '<td>' . $row['field13'] . '</td>';
                    echo '<td>' . $row['field11'] . '</td>';
                    echo '<td>' . $row['zavnum'] . '</td>';
                    echo '<td>' . $row['longg'] . '</td>';
                    echo '<td>' . $row['price'] . '</td>';
                    echo '<td>' . $ispayeed . '</td>';
                    echo '<td>' . date("d/m/Y H:i:s", $row['date']) . '</td>';
                    echo '<td>
		         <div class="btn-group-vertical">
                  <a onclick="delit(' . $row['id'] . '); return false;" class="btn btn-danger btn-sm change_f" href="?delete&id=' . $row['id'] . '"><span class="glyphicon glyphicon-remove"></span>&nbsp;Удалить</a>		
				  <a class="btn btn-default btn-sm change_f" href="?renameorder&reqid=' . $row['id'] . '"><span class="glyphicon glyphicon-pencil"></span>&nbsp;Изменить</a>	
                  <a href="test.php?fio=' . $row['field1'] . ' ' . $row['field2'] . ' ' . $row['field3'] . '&price=' . $row['price'] . '&auto=' . $row['field6'] . ' ' . $row['field7'] . '&num=' . $row['field4'] . '&date=' . date("d/m/Y H:i:s", $row['date']) . '" class="btn btn-info btn-sm save_check_pdf"><span class="glyphicon glyphicon-floppy-save"></span>&nbsp;Скачать чек</a>			
				  ' . $linkpay . '
				 </div>
				</td>';
                    echo '</tr>';
                }
            }
            echo '</table><br>';
            echo '</center></div>';
        }
    }
Example #12
0
 /**
  * Find the position of the last occurrence of a substring in a string
  * UTF-8 ONLY safe strrpos(), uses mbstring, falls back to iconv.
  *
  * @param string $haystack the string to search in
  * @param string $needle one or more charachters to search for
  * @return int the numeric position of the last occurrence of needle in haystack
  */
 public static function strrpos($haystack, $needle) {
     if (function_exists('mb_strpos')) {
         return mb_strrpos($haystack, $needle, null, 'UTF-8');
     } else {
         return iconv_strrpos($haystack, $needle, 'UTF-8');
     }
 }
Example #13
0
 function mb_strrpos($haystack, $needle, $encoding = '')
 {
     $encoding = $this->regularize_encoding($encoding);
     if ($this->use_iconv) {
         return iconv_strrpos($haystack, $needle, $encoding);
     } else {
         switch ($e = $this->mbemu_internals['encoding'][$encoding]) {
             case 1:
                 //euc-jp
             //euc-jp
             case 2:
                 //shift-jis
             //shift-jis
             case 4:
                 //utf-8
             //utf-8
             case 5:
                 //utf-16
             //utf-16
             case 8:
                 //utf16BE
                 preg_match_all('/' . $this->mbemu_internals['regex'][$e] . '/', $haystack, $ar_h);
                 preg_match_all('/' . $this->mbemu_internals['regex'][$e] . '/', $needle, $ar_n);
                 return $this->_sub_strrpos($ar_h[0], $ar_n[0]);
             case 3:
                 //jis
                 $haystack = $this->mb_convert_encoding($haystack, 'SJIS', 'JIS');
                 $needle = $this->mb_convert_encoding($needle, 'SJIS', 'JIS');
                 preg_match_all('/' . $this->mbemu_internals['regex'][2] . '/', $haystack, $ar_h);
                 preg_match_all('/' . $this->mbemu_internals['regex'][2] . '/', $needle, $ar_n);
                 return $this->_sub_strrpos($ar_h[0], $ar_n[0]);
             case 0:
                 //ascii
             //ascii
             case 6:
                 //iso-8859-1
             //iso-8859-1
             default:
                 return strrpos($haystack, $needle);
         }
     }
 }
Example #14
0
 private static function grapheme_position($s, $needle, $offset, $mode)
 {
     if (!preg_match('/./us', $needle .= '')) {
         return false;
     }
     if (!preg_match('/./us', $s .= '')) {
         return false;
     }
     if ($offset > 0) {
         $s = self::grapheme_substr($s, $offset);
     } elseif ($offset < 0) {
         if (defined('HHVM_VERSION_ID') || PHP_VERSION_ID < 50535 || 50600 <= PHP_VERSION_ID && PHP_VERSION_ID < 50621 || 70000 <= PHP_VERSION_ID && PHP_VERSION_ID < 70006) {
             $offset = 0;
         } else {
             return false;
         }
     }
     switch ($mode) {
         case 0:
             $needle = iconv_strpos($s, $needle, 0, 'UTF-8');
             break;
         case 1:
             $needle = mb_stripos($s, $needle, 0, 'UTF-8');
             break;
         case 2:
             $needle = iconv_strrpos($s, $needle, 'UTF-8');
             break;
         default:
             $needle = mb_strripos($s, $needle, 0, 'UTF-8');
             break;
     }
     return $needle ? self::grapheme_strlen(iconv_substr($s, 0, $needle, 'UTF-8')) + $offset : $needle;
 }
Example #15
0
/**
 * nv_strrpos()
 * 
 * @param mixed $haystack
 * @param mixed $needle
 * @param integer $offset
 * @return
 */
function nv_strrpos($haystack, $needle, $offset = 0)
{
    global $global_config;
    return iconv_strrpos($haystack, $needle, $offset, $global_config['site_charset']);
}
Example #16
0
<?php

/* Prototype  : proto int iconv_strrpos(string haystack, string needle [, string charset])
 * Description: Find position of last occurrence of a string within another 
 * Source code: ext/iconv/iconv.c
 */
/*
 * Pass iconv_strrpos() an encoding that doesn't exist
 */
echo "*** Testing iconv_strrpos() : error conditions ***\n";
$haystack = 'This is an English string. 0123456789.';
$needle = '123';
$offset = 5;
$encoding = 'unknown-encoding';
var_dump(iconv_strrpos($haystack, $needle, $encoding));
echo "Done";
/**
 * utf8_strrpos( )
 * 
 * Find position of last occurrence of a char in a UTF-8 string
 * @since 1.3
 * 
 * @param    string $haystack The string to search in
 * @param    string $needle The string to search for
 * @param    int $offset Number of char to ignore from start or end
 * @return   int THe position of last occurrance of needle
 */
function utf8_strrpos($haystack, $needle, $offset = 0)
{
    if ((int) $needle === $needle && $needle >= 0) {
        $needle = utf8_chr($needle);
    }
    $needle = utf8_clean((string) $needle);
    $offset = (int) $offset;
    $haystack = utf8_clean($haystack);
    if (mbstring_loaded()) {
        //mb_strrpos returns wrong position if invalid characters are found in $haystack before $needle
        return mb_strrpos($haystack, $needle, $offset, 'UTF-8');
    }
    if (iconv_loaded() && $offset === 0) {
        //iconv_strrpos is not tolerant to invalid characters
        //iconv_strrpos does not accept $offset
        return iconv_strrpos($haystack, $needle, 'UTF-8');
    }
    if ($offset > 0) {
        $haystack = utf8_substr($haystack, $offset);
    } else {
        if ($offset < 0) {
            $haystack = utf8_substr($haystack, 0, $offset);
        }
    }
    if (($pos = strrpos($haystack, $needle)) !== false) {
        $left = substr($haystack, 0, $pos);
        return ($offset > 0 ? $offset : 0) + utf8_strlen($left);
    }
    return false;
}
Example #18
0
 /**
  * Find position of a string
  *
  * IndexOf mode is one of the following:
  *  - {@link AeString::INDEX_LEFT}  - (default) search from left to right
  *  - {@link AeString::INDEX_RIGHT} - search from right to left
  *
  * This method returns scalar integer instead of {@link AeInteger} instance.
  * This is due to the fact, that the method does not manipulate the string
  * in any way but is informational
  *
  * This method uses iconv and mb_string functions, when available, to detect
  * the index of a needle in a UTF-8 encoded multibyte string
  *
  * @see strpos(), stripos(), strrpost(), strripos()
  *
  * @uses AeString::INDEX_LEFT
  * @uses AeString::INDEX_RIGHT
  *
  * @param string $needle
  * @param int    $offset
  * @param int    $mode
  * @param bool   $case_sensitive
  *
  * @return int
  */
 public function indexOf($needle, $offset = 0, $mode = AeString::INDEX_LEFT, $case_sensitive = true)
 {
     if ($needle instanceof AeScalar) {
         $needle = $needle->toString()->getValue();
     }
     if ($offset instanceof AeScalar) {
         $offset = $offset->toInteger()->getValue();
     }
     if ($case_sensitive instanceof AeScalar) {
         $case_sensitive = $case_sensitive->toBoolean()->getValue();
     }
     if ($mode == AeString::INDEX_RIGHT) {
         if ($case_sensitive === false) {
             if (function_exists('mb_strripos')) {
                 return mb_strripos($this->_value, $needle, $offset, 'UTF-8');
             }
             return strripos($this->_value, $needle, $offset);
         }
         if ($offset !== 0) {
             if (function_exists('mb_strrpos')) {
                 // *** Use mb_string function instead
                 return mb_strrpos($this->_value, $needle, $offset, 'UTF-8');
             }
             // *** Emulate $offset parameter for iconv_strrpos
             if ($offset > 0) {
                 $string = iconv_substr($this->_value, $offset, $this->length, 'UTF-8');
             } else {
                 $string = iconv_substr($this->_value, 0, $offset, 'UTF-8');
                 $offset = 0;
             }
             return iconv_strrpos($string, $needle, 'UTF-8') + $offset;
         }
         return iconv_strrpos($this->_value, $needle, 'UTF-8');
     }
     if ($case_sensitive === false) {
         if (function_exists('mb_stripos')) {
             return mb_stripos($this->_value, $needle, $offset, 'UTF-8');
         }
         return stripos($this->_value, $needle, $offset);
     }
     return iconv_strpos($this->_value, $needle, $offset, 'UTF-8');
 }
Example #19
0
function dle_strrpos($str, $needle, $charset)
{
    if (strtolower($charset) == "utf-8") {
        if (function_exists('mb_strrpos')) {
            return mb_strrpos($str, $needle, null, "utf-8");
        } elseif (function_exists('iconv_strrpos')) {
            return iconv_strrpos($str, $needle, "utf-8");
        }
    }
    return strrpos($str, $needle);
}
 static function mb_strrchr($haystack, $needle, $part = false, $encoding = INF)
 {
     $needle = self::mb_substr($needle, 0, 1, $encoding);
     $pos = iconv_strrpos($haystack, $needle, $encoding);
     return self::getSubpart($pos, $part, $haystack, $encoding);
 }
 /**
  * Find position of last occurrence of a string
  *
  * @param string $haystack
  * @param string $needle
  * @param int $offset
  * @return int|false
  */
 public function strrpos($haystack, $needle, $offset = null)
 {
     return iconv_strrpos($haystack, $needle, $offset, self::ICONV_CHARSET);
 }
Example #22
0
 /**
  * Returns a locale formatted number
  * 
  * @param  string              $value      Number to localize
  * @param  integer             $precision  OPTIONAL Precision of a float value, not touched if null
  * @param  string|Zend_Locale  $locale     OPTIONAL Locale for parsing
  * @return string  locale formatted number
  */
 public static function toNumber($value, $precision = null, $locale = null)
 {
     if ($locale == null and Zend_Locale::isLocale($precision)) {
         $locale = $precision;
         $precision = null;
     }
     // Get correct signs for this locale
     $symbols = Zend_Locale_Data::getContent($locale, 'numbersymbols');
     $format = Zend_Locale_Data::getContent($locale, 'decimalnumberformat');
     $format = $format['default'];
     iconv_set_encoding('internal_encoding', 'UTF-8');
     // seperate negative format pattern when avaiable
     if (iconv_strpos($format, ';') !== false) {
         if (call_user_func(Zend_Locale_Math::$comp, $value, 0) < 0) {
             $format = iconv_substr($format, iconv_strpos($format, ';') + 1);
         } else {
             $format = iconv_substr($format, 0, iconv_strpos($format, ';'));
         }
     }
     // set negative sign
     if (call_user_func(Zend_Locale_Math::$comp, $value, 0) < 0) {
         if (iconv_strpos($format, '-') === false) {
             $format = $symbols['minus'] . $format;
         } else {
             $format = str_replace('-', $symbols['minus'], $format);
         }
     }
     // get number parts
     if (iconv_strpos($value, '.') !== false) {
         if ($precision === null) {
             $precision = iconv_substr($value, iconv_strpos($value, '.') + 1);
         } else {
             $precision = iconv_substr($value, iconv_strpos($value, '.') + 1, $precision);
         }
     } else {
         $precision = '';
     }
     // get fraction and format lengths
     call_user_func(Zend_Locale_Math::$scale, iconv_strlen($precision));
     $prec = call_user_func(Zend_Locale_Math::$sub, $value, call_user_func(Zend_Locale_Math::$sub, $value, '0', 0));
     if (iconv_strpos($prec, '-') !== false) {
         $prec = iconv_substr($prec, 1);
     }
     $number = call_user_func(Zend_Locale_Math::$sub, $value, 0, 0);
     if (iconv_strpos($number, '-') !== false) {
         $number = iconv_substr($number, 1);
     }
     $group = iconv_strrpos($format, ',');
     $group2 = iconv_strpos($format, ',');
     $point = iconv_strpos($format, '.');
     // Add fraction
     if ($prec == 0) {
         $format = iconv_substr($format, 0, $point) . iconv_substr($format, iconv_strrpos($format, '#') + 1);
     } else {
         $format = iconv_substr($format, 0, $point) . $symbols['decimal'] . iconv_substr($prec, 2) . iconv_substr($format, iconv_strrpos($format, '#') + 1);
     }
     // Add seperation
     if ($group == 0) {
         // no seperation
         $format = $number . iconv_substr($format, $point);
     } else {
         if ($group == $group2) {
             // only 1 seperation
             $seperation = $point - $group - 1;
             for ($x = iconv_strlen($number); $x > $group2; $x -= $seperation) {
                 if (iconv_substr($number, 0, $x - $seperation) !== "") {
                     $number = iconv_substr($number, 0, $x - $seperation) . $symbols['group'] . iconv_substr($number, $x - $seperation);
                 }
             }
             $format = iconv_substr($format, 0, iconv_strpos($format, '#')) . $number . iconv_substr($format, $point);
         } else {
             // 2 seperations
             if (iconv_strlen($number) > $point - $group - 1) {
                 $seperation = $point - $group - 1;
                 $number = iconv_substr($number, 0, iconv_strlen($number) - $seperation) . $symbols['group'] . iconv_substr($number, iconv_strlen($number) - $seperation);
                 if (iconv_strlen($number) - 1 > $point - $group) {
                     $seperation2 = $group - $group2 - 1;
                     for ($x = iconv_strlen($number) - $seperation2 - 2; $x > $seperation2; $x -= $seperation2) {
                         $number = iconv_substr($number, 0, $x - $seperation2) . $symbols['group'] . iconv_substr($number, $x - $seperation2);
                     }
                 }
             }
             $format = iconv_substr($format, 0, iconv_strpos($format, '#')) . $number . iconv_substr($format, $point);
         }
     }
     return (string) $format;
 }
Example #23
0
 private static function grapheme_position($s, $needle, $offset, $mode)
 {
     if (!preg_match('/./us', $needle .= '')) {
         return false;
     }
     if (!preg_match('/./us', $s .= '')) {
         return false;
     }
     if ($offset > 0) {
         $s = self::grapheme_substr($s, $offset);
     } elseif ($offset < 0) {
         $offset = 0;
     }
     switch ($mode) {
         case 0:
             $needle = iconv_strpos($s, $needle, 0, 'UTF-8');
             break;
         case 1:
             $needle = mb_stripos($s, $needle, 0, 'UTF-8');
             break;
         case 2:
             $needle = iconv_strrpos($s, $needle, 'UTF-8');
             break;
         default:
             $needle = mb_strripos($s, $needle, 0, 'UTF-8');
             break;
     }
     return $needle ? self::grapheme_strlen(iconv_substr($s, 0, $needle, 'UTF-8')) + $offset : $needle;
 }
Example #24
0
 /**
  * Returns a locale formatted number depending on the given options.
  * The seperation and fraction sign is used from the set locale.
  * ##0.#  -> 12345.12345 -> 12345.12345
  * ##0.00 -> 12345.12345 -> 12345.12
  * ##,##0.00 -> 12345.12345 -> 12,345.12
  *
  * @param   string  $input    Localized number string
  * @param   array   $options  Options: number_format, locale, precision. See {@link setOptions()} for details.
  * @return  string  locale formatted number
  */
 public static function toNumber($value, array $options = array())
 {
     print "\ntoNumber";
     print "\n--------";
     // load class within method for speed
     require_once 'Zend/Locale/Math.php';
     $value = Zend_Locale_Math::normalize($value);
     print "\n1-Value :'" . $value . "'";
     $options = array_merge(self::$_Options, self::checkOptions($options));
     if ($options['locale'] instanceof Zend_Locale) {
         $options['locale'] = $options['locale']->toString();
     }
     print "\n2-Locale:'" . $options['locale'] . "'";
     // Get correct signs for this locale
     $symbols = Zend_Locale_Data::getList($options['locale'], 'symbols');
     iconv_set_encoding('internal_encoding', 'UTF-8');
     // Get format
     $format = $options['number_format'];
     print "\n3-Format1:'" . $format . "'";
     if ($format === null) {
         $format = Zend_Locale_Data::getContent($options['locale'], 'decimalnumber');
         print "\n3-Format2:'" . $format . "'";
         if (iconv_strpos($format, ';') !== false) {
             if (call_user_func(Zend_Locale_Math::$comp, $value, 0) < 0) {
                 $format = iconv_substr($format, iconv_strpos($format, ';') + 1);
                 print "\n3-Format3:'" . $format . "'";
             } else {
                 $format = iconv_substr($format, 0, iconv_strpos($format, ';'));
                 print "\n3-Format4:'" . $format . "'";
             }
         }
     } else {
         print "\n3-Format5:'" . $format . "'";
         // seperate negative format pattern when avaiable
         if (iconv_strpos($format, ';') !== false) {
             if (call_user_func(Zend_Locale_Math::$comp, $value, 0) < 0) {
                 $format = iconv_substr($format, iconv_strpos($format, ';') + 1);
                 print "\n3-Format6:'" . $format . "'";
             } else {
                 $format = iconv_substr($format, 0, iconv_strpos($format, ';'));
                 print "\n3-Format7:'" . $format . "'";
             }
         }
         if (strpos($format, '.')) {
             print "\n4-Prec:'" . $options['precision'] . "'";
             if (is_numeric($options['precision'])) {
                 $value = Zend_Locale_Math::round($value, $options['precision']);
                 print "\n4-Value1:'" . $value . "'";
                 //print "\nSet Pr:'".$value."'";
             } else {
                 if (substr($format, strpos($format, '.') + 1, 3) == '###') {
                     $options['precision'] = null;
                     print "\n4-Value2";
                 } else {
                     $options['precision'] = strlen(substr($format, strpos($format, '.') + 1, strrpos($format, '0') - strpos($format, '.')));
                     print "\n4-Prec3:'" . $options['precision'] . "'";
                     $format = substr($format, 0, strpos($format, '.') + 1) . '###' . substr($format, strrpos($format, '0') + 1);
                     print "\n4-Format4:'" . $format . "'";
                 }
             }
         } else {
             $value = Zend_Locale_Math::round($value, 0);
             $options['precision'] = 0;
             print "\n4-Value5:'" . $value . "'";
         }
         $value = Zend_Locale_Math::normalize($value);
         print "\n5-Value:'" . $value . "'";
     }
     // get number parts
     if (strlen($value) != strlen(Zend_Locale_Math::round($value, 0))) {
         if ($options['precision'] === null) {
             $precstr = iconv_substr($value, strlen(Zend_Locale_Math::round($value, 0)) + 1);
             print "\n6-Prec1:'" . $precstr . "'";
         } else {
             $precstr = iconv_substr($value, strlen(Zend_Locale_Math::round($value, 0)) + 1, $options['precision']);
             print "\n6-Prec2:'" . $precstr . "'";
             if (iconv_strlen($precstr) < $options['precision']) {
                 $precstr = $precstr . str_pad("0", $options['precision'] - iconv_strlen($precstr), "0");
                 print "\n6-Prec3:'" . $precstr . "'";
             }
         }
     } else {
         print "\n6-Prec4";
         if ($options['precision'] > 0) {
             $precstr = str_pad("0", $options['precision'], "0");
             print "\n6-Prec5:'" . $precstr . "'";
         }
     }
     if ($options['precision'] === null) {
         if (isset($precstr)) {
             $options['precision'] = iconv_strlen($precstr);
             print "\n7-Prec1:'" . $options['precision'] . "'";
         } else {
             $options['precision'] = 0;
             print "\n7-Prec2:'" . $options['precision'] . "'";
         }
     }
     // get fraction and format lengths
     if (strpos($value, '.') !== false) {
         $number = substr((string) $value, 0, strpos($value, '.'));
         print "\n8-Number1:'" . $number . "'";
     } else {
         $number = $value;
         print "\n8-Number2:'" . $number . "'";
     }
     $prec = call_user_func(Zend_Locale_Math::$sub, $value, $number, $options['precision']);
     print "\n9-Prec1:'" . $prec . "'";
     if (iconv_strpos($prec, '-') !== false) {
         $prec = iconv_substr($prec, 1);
         print "\n9-Prec2:'" . $prec . "'";
     }
     if ($prec == 0 and $options['precision'] > 0) {
         $prec = "0.0";
         print "\n9-Prec3:'" . $prec . "'";
     }
     if ($options['precision'] + 2 > iconv_strlen($prec)) {
         $prec = str_pad((string) $prec, $options['precision'] + 2, "0", STR_PAD_RIGHT);
         print "\n9-Prec4:'" . $prec . "'";
     }
     if (iconv_strpos($number, '-') !== false) {
         $number = iconv_substr($number, 1);
         print "\n10-Number:'" . $number . "'";
     }
     $group = iconv_strrpos($format, ',');
     $group2 = iconv_strpos($format, ',');
     $point = iconv_strpos($format, '0');
     // Add fraction
     $rest = "";
     if ($value < 0 && strpos($format, '.')) {
         $rest = substr(substr($format, strpos($format, '.') + 1), -1, 1);
         print "\n11-Rest:'" . $rest . "'";
     }
     if ($options['precision'] == '0') {
         $format = iconv_substr($format, 0, $point) . iconv_substr($format, iconv_strrpos($format, '#') + 2);
         print "\n12-Format1:'" . $format . "'";
     } else {
         $format = iconv_substr($format, 0, $point) . $symbols['decimal'] . iconv_substr($prec, 2) . iconv_substr($format, iconv_strrpos($format, '#') + 2 + strlen($prec) - 2);
         print "\n12-Format2:'" . $format . "'";
     }
     if ($value < 0 and $rest != '0' and $rest != '#') {
         $format .= $rest;
         print "\n12-Format3:'" . $format . "'";
     }
     // Add seperation
     if ($group == 0) {
         // no seperation
         $format = $number . iconv_substr($format, $point);
         print "\n12-Format4:'" . $format . "'";
     } else {
         if ($group == $group2) {
             // only 1 seperation
             $seperation = $point - $group;
             for ($x = iconv_strlen($number); $x > $seperation; $x -= $seperation) {
                 if (iconv_substr($number, 0, $x - $seperation) !== "") {
                     $number = iconv_substr($number, 0, $x - $seperation) . $symbols['group'] . iconv_substr($number, $x - $seperation);
                     print "\n12-Number5:'" . $number . "'";
                 }
             }
             $format = iconv_substr($format, 0, iconv_strpos($format, '#')) . $number . iconv_substr($format, $point);
             print "\n12-Format6:'" . $format . "'";
         } else {
             // 2 seperations
             if (iconv_strlen($number) > $point - $group) {
                 $seperation = $point - $group;
                 $number = iconv_substr($number, 0, iconv_strlen($number) - $seperation) . $symbols['group'] . iconv_substr($number, iconv_strlen($number) - $seperation);
                 print "\n13-Number1:'" . $number . "'";
                 if (iconv_strlen($number) - 1 > $point - $group + 1) {
                     $seperation2 = $group - $group2 - 1;
                     for ($x = iconv_strlen($number) - $seperation2 - 2; $x > $seperation2; $x -= $seperation2) {
                         $number = iconv_substr($number, 0, $x - $seperation2) . $symbols['group'] . iconv_substr($number, $x - $seperation2);
                         print "\n13-Number2:'" . $number . "'";
                     }
                 }
             }
             $format = iconv_substr($format, 0, iconv_strpos($format, '#')) . $number . iconv_substr($format, $point);
             print "\n12-Format6:'" . $format . "'";
         }
     }
     // set negative sign
     if (call_user_func(Zend_Locale_Math::$comp, $value, 0) < 0) {
         if (iconv_strpos($format, '-') === false) {
             $format = $symbols['minus'] . $format;
         } else {
             $format = str_replace('-', $symbols['minus'], $format);
         }
     }
     print "\nRESULT:'" . $format . "'";
     return (string) $format;
 }
 function utf8_strrpos($string, $needle)
 {
     return iconv_strrpos($string, $needle, 'UTF-8');
 }
Example #26
0
 /**
  * Returns a locale formatted number depending on the given options.
  * The seperation and fraction sign is used from the set locale.
  * ##0.#  -> 12345.12345 -> 12345.12345
  * ##0.00 -> 12345.12345 -> 12345.12
  * ##,##0.00 -> 12345.12345 -> 12,345.12
  * 
  * @param   string  $input    Localized number string
  * @param   array   $options  Options: number_format, locale, precision. See {@link setOptions()} for details.
  * @return  string  locale formatted number
  */
 public static function toNumber($value, array $options = array())
 {
     $value = Zend_Locale_Math::normalize($value);
     $options = array_merge(self::$_Options, self::checkOptions($options));
     if ($options['locale'] instanceof Zend_Locale) {
         $options['locale'] = $options['locale']->toString();
     }
     // Get correct signs for this locale
     $symbols = Zend_Locale_Data::getContent($options['locale'], 'numbersymbols');
     iconv_set_encoding('internal_encoding', 'UTF-8');
     // Get format
     $format = $options['number_format'];
     if ($format === null) {
         $format = Zend_Locale_Data::getContent($options['locale'], 'decimalnumberformat');
         $format = $format['default'];
         if (iconv_strpos($format, ';') !== false) {
             if (call_user_func(Zend_Locale_Math::$comp, $value, 0) < 0) {
                 $format = iconv_substr($format, iconv_strpos($format, ';') + 1);
             } else {
                 $format = iconv_substr($format, 0, iconv_strpos($format, ';'));
             }
         }
     } else {
         // seperate negative format pattern when avaiable
         if (iconv_strpos($format, ';') !== false) {
             if (call_user_func(Zend_Locale_Math::$comp, $value, 0) < 0) {
                 $format = iconv_substr($format, iconv_strpos($format, ';') + 1);
             } else {
                 $format = iconv_substr($format, 0, iconv_strpos($format, ';'));
             }
         }
         if (strpos($format, '.')) {
             if (is_numeric($options['precision'])) {
                 $value = round($value, $options['precision']);
             } else {
                 if (substr($format, strpos($format, '.') + 1, 3) == '###') {
                     $options['precision'] = null;
                 } else {
                     $options['precision'] = strlen(substr($format, strpos($format, '.') + 1, strrpos($format, '0') - strpos($format, '.')));
                     $format = substr($format, 0, strpos($format, '.') + 1) . '###' . substr($format, strrpos($format, '0') + 1);
                 }
             }
         } else {
             $value = round($value, 0);
             $options['precision'] = 0;
         }
     }
     // set negative sign
     if (call_user_func(Zend_Locale_Math::$comp, $value, 0) < 0) {
         if (iconv_strpos($format, '-') === false) {
             $format = $symbols['minus'] . $format;
         } else {
             $format = str_replace('-', $symbols['minus'], $format);
         }
     }
     // get number parts
     if (strlen($value) != strlen(round($value, 0))) {
         if ($options['precision'] === null) {
             $precstr = iconv_substr($value, strlen(round($value, 0)) + 1);
         } else {
             $precstr = iconv_substr($value, strlen(round($value, 0)) + 1, $options['precision']);
             if (iconv_strlen($precstr) < $options['precision']) {
                 $precstr = $precstr . str_pad("0", $options['precision'] - iconv_strlen($precstr), "0");
             }
         }
     } else {
         if ($options['precision'] > 0) {
             $precstr = str_pad("0", $options['precision'], "0");
         }
     }
     if ($options['precision'] === null) {
         if (isset($precstr)) {
             $options['precision'] = iconv_strlen($precstr);
         } else {
             $options['precision'] = 0;
         }
     }
     // get fraction and format lengths
     $preg = call_user_func(Zend_Locale_Math::$sub, $value, '0', 0);
     $prec = call_user_func(Zend_Locale_Math::$sub, $value, $preg, $options['precision']);
     if (iconv_strpos($prec, '-') !== false) {
         $prec = iconv_substr($prec, 1);
     }
     if ($options['precision'] + 2 > strlen($prec)) {
         $prec = $prec . str_pad("0", $options['precision'] - iconv_strlen($prec), "0");
     }
     $number = call_user_func(Zend_Locale_Math::$sub, $value, $prec, 0);
     if (iconv_strpos($number, '-') !== false) {
         $number = iconv_substr($number, 1);
     }
     $group = iconv_strrpos($format, ',');
     $group2 = iconv_strpos($format, ',');
     $point = iconv_strpos($format, '0');
     // Add fraction
     $rest = "";
     if ($value < 0 && strpos($format, '.')) {
         $rest = substr(substr($format, strpos($format, '.') + 1), -1, 1);
     }
     if ($options['precision'] == '0') {
         $format = iconv_substr($format, 0, $point) . iconv_substr($format, iconv_strrpos($format, '#') + 2);
     } else {
         $format = iconv_substr($format, 0, $point) . $symbols['decimal'] . iconv_substr($prec, 2) . iconv_substr($format, iconv_strrpos($format, '#') + 2);
     }
     if ($value < 0 and $rest != '0' and $rest != '#') {
         $format .= $rest;
     }
     // Add seperation
     if ($group == 0) {
         // no seperation
         $format = $number . iconv_substr($format, $point);
     } else {
         if ($group == $group2) {
             // only 1 seperation
             $seperation = $point - $group;
             for ($x = iconv_strlen($number); $x > $seperation; $x -= $seperation) {
                 if (iconv_substr($number, 0, $x - $seperation) !== "") {
                     $number = iconv_substr($number, 0, $x - $seperation) . $symbols['group'] . iconv_substr($number, $x - $seperation);
                 }
             }
             $format = iconv_substr($format, 0, iconv_strpos($format, '#')) . $number . iconv_substr($format, $point);
         } else {
             // 2 seperations
             if (iconv_strlen($number) > $point - $group) {
                 $seperation = $point - $group;
                 $number = iconv_substr($number, 0, iconv_strlen($number) - $seperation) . $symbols['group'] . iconv_substr($number, iconv_strlen($number) - $seperation);
                 if (iconv_strlen($number) - 1 > $point - $group + 1) {
                     $seperation2 = $group - $group2 - 1;
                     for ($x = iconv_strlen($number) - $seperation2 - 2; $x > $seperation2; $x -= $seperation2) {
                         $number = iconv_substr($number, 0, $x - $seperation2) . $symbols['group'] . iconv_substr($number, $x - $seperation2);
                     }
                 }
             }
             $format = iconv_substr($format, 0, iconv_strpos($format, '#')) . $number . iconv_substr($format, $point);
         }
     }
     return (string) $format;
 }
Example #27
0
    /**
     * Returns a locale formatted number depending on the given options.
     * The seperation and fraction sign is used from the set locale.
     * ##0.#  -> 12345.12345 -> 12345.12345
     * ##0.00 -> 12345.12345 -> 12345.12
     * ##,##0.00 -> 12345.12345 -> 12,345.12
     *
     * @param   string  $input    Localized number string
     * @param   array   $options  Options: number_format, locale, precision. See {@link setOptions()} for details.
     * @return  string  locale formatted number
     */
    public static function toNumber($value, array $options = array())
    {
        // load class within method for speed
        require_once 'Zend/Locale/Math.php';

        $value = Zend_Locale_Math::normalize($value);
        $options = array_merge(self::$_Options, self::checkOptions($options));
        if ($options['locale'] instanceof Zend_Locale) {
            $options['locale'] = $options['locale']->toString();
        }

        // Get correct signs for this locale
        $symbols = Zend_Locale_Data::getList($options['locale'], 'symbols');
        iconv_set_encoding('internal_encoding', 'UTF-8');

        // Get format
        $format = $options['number_format'];
        if ($format === null) {
            $format  = Zend_Locale_Data::getContent($options['locale'], 'decimalnumber');
            if (iconv_strpos($format, ';') !== false) {
                if (call_user_func(Zend_Locale_Math::$comp, $value, 0) < 0) {
                    $format = iconv_substr($format, iconv_strpos($format, ';') + 1);
                } else {
                    $format = iconv_substr($format, 0, iconv_strpos($format, ';'));
                }
            }
        } else {
            // seperate negative format pattern when available
            if (iconv_strpos($format, ';') !== false) {
                if (call_user_func(Zend_Locale_Math::$comp, $value, 0) < 0) {
                    $format = iconv_substr($format, iconv_strpos($format, ';') + 1);
                } else {
                    $format = iconv_substr($format, 0, iconv_strpos($format, ';'));
                }
            }

            if (strpos($format, '.')) {
                if (is_numeric($options['precision'])) {
                    $value = Zend_Locale_Math::round($value, $options['precision']);
                } else {
                    if (substr($format, strpos($format, '.') + 1, 3) == '###') {
                        $options['precision'] = null;
                    } else {
                        $options['precision'] = strlen(substr($format, strpos($format, '.') + 1,
                                                              strrpos($format, '0') - strpos($format, '.')));
                        $format = substr($format, 0, strpos($format, '.') + 1) . '###'
                                . substr($format, strrpos($format, '0') + 1);
                    }
                }
            } else {
                $value = Zend_Locale_Math::round($value, 0);
                $options['precision'] = 0;
            }
            $value = Zend_Locale_Math::normalize($value);
        }

        // get number parts
        if (strlen($value) != strlen(Zend_Locale_Math::round($value, 0))) {
            if ($options['precision'] === null) {
                $precstr = iconv_substr($value, strlen(Zend_Locale_Math::round($value, 0)) + 1);
            } else {
                $precstr = iconv_substr($value, strlen(Zend_Locale_Math::round($value, 0)) + 1, $options['precision']);
                if (iconv_strlen($precstr) < $options['precision']) {
                    $precstr = $precstr . str_pad("0", ($options['precision'] - iconv_strlen($precstr)), "0");
                }
            }
        } else {
            if ($options['precision'] > 0) {
                $precstr = str_pad("0", ($options['precision']), "0");
            }
        }

        if ($options['precision'] === null) {
            if (isset($precstr)) {
                $options['precision'] = iconv_strlen($precstr);
            } else {
                $options['precision'] = 0;
            }
        }

        // get fraction and format lengths
        if (strpos($value, '.') !== false) {
            $number = substr((string) $value, 0, strpos($value, '.'));
        } else {
            $number = $value;
        }

        $prec   = call_user_func(Zend_Locale_Math::$sub, $value, $number, $options['precision']);
        if (iconv_strpos($prec, '-') !== false) {
            $prec = iconv_substr($prec, 1);
        }
        if (($prec == 0) and ($options['precision'] > 0)) {
            $prec = "0.0";
        }
        if (($options['precision'] + 2) > iconv_strlen($prec)) {
            $prec = str_pad((string) $prec, $options['precision'] + 2, "0", STR_PAD_RIGHT);
        }
        if (iconv_strpos($number, '-') !== false) {
            $number = iconv_substr($number, 1);
        }
        $group  = iconv_strrpos($format, ',');
        $group2 = iconv_strpos ($format, ',');
        $point  = iconv_strpos ($format, '0');
        // Add fraction
        $rest = "";
        if (($value < 0) && (strpos($format, '.'))) {
            $rest   = substr(substr($format, strpos($format, '.') + 1), -1, 1);
        }
        if ($options['precision'] == '0') {
            $format = iconv_substr($format, 0, $point) . iconv_substr($format, iconv_strrpos($format, '#') + 2);
        } else {
            $format = iconv_substr($format, 0, $point) . $symbols['decimal']
                               . iconv_substr($prec, 2) . iconv_substr($format, iconv_strrpos($format, '#') + 2 + strlen($prec) - 2);
        }
        if (($value < 0) and ($rest != '0') and ($rest != '#')) {
            $format .= $rest;
        }
        // Add seperation
        if ($group == 0) {
            // no seperation
            $format = $number . iconv_substr($format, $point);
        } else if ($group == $group2) {
            // only 1 seperation
            $seperation = ($point - $group);
            for ($x = iconv_strlen($number); $x > $seperation; $x -= $seperation) {
                if (iconv_substr($number, 0, $x - $seperation) !== "") {
                    $number = iconv_substr($number, 0, $x - $seperation) . $symbols['group']
                             . iconv_substr($number, $x - $seperation);
                }
            }
            $format = iconv_substr($format, 0, iconv_strpos($format, '#')) . $number . iconv_substr($format, $point);
        } else {

            // 2 seperations
            if (iconv_strlen($number) > ($point - $group)) {
                $seperation = ($point - $group);
                $number = iconv_substr($number, 0, iconv_strlen($number) - $seperation) . $symbols['group']
                        . iconv_substr($number, iconv_strlen($number) - $seperation);

                if ((iconv_strlen($number) - 1) > ($point - $group + 1)) {
                    $seperation2 = ($group - $group2 - 1);

                    for ($x = iconv_strlen($number) - $seperation2 - 2; $x > $seperation2; $x -= $seperation2) {
                         $number = iconv_substr($number, 0, $x - $seperation2) . $symbols['group']
                                 . iconv_substr($number, $x - $seperation2);
                    }
                }

            }
            $format = iconv_substr($format, 0, iconv_strpos($format, '#')) . $number . iconv_substr($format, $point);
        }
        // set negative sign
        if (call_user_func(Zend_Locale_Math::$comp, $value, 0) < 0) {
            if (iconv_strpos($format, '-') === false) {
                $format = $symbols['minus'] . $format;
            } else {
                $format = str_replace('-', $symbols['minus'], $format);
            }
        }
        return (string) $format;
    }
Example #28
0
<?php

/* Prototype  : proto int iconv_strrpos(string haystack, string needle [, string charset])
 * Description: Find position of last occurrence of a string within another 
 * Source code: ext/iconv/iconv.c
 */
/*
 * Pass iconv_strrpos() an incorrect number of arguments
 */
echo "*** Testing iconv_strrpos() : error conditions ***\n";
//Test iconv_strrpos with one more than the expected number of arguments
echo "\n-- Testing iconv_strrpos() function with more than expected no. of arguments --\n";
$haystack = 'string_val';
$needle = 'string_val';
$encoding = 'string_val';
$extra_arg = 10;
var_dump(iconv_strrpos($haystack, $needle, $encoding, $extra_arg));
// Testing iconv_strrpos with one less than the expected number of arguments
echo "\n-- Testing iconv_strrpos() function with less than expected no. of arguments --\n";
$haystack = 'string_val';
var_dump(iconv_strrpos($haystack));
echo "Done";
 /**
  * Returns a locale formatted number depending on the given options.
  * The seperation and fraction sign is used from the set locale.
  * ##0.#  -> 12345.12345 -> 12345.12345
  * ##0.00 -> 12345.12345 -> 12345.12
  * ##,##0.00 -> 12345.12345 -> 12,345.12
  *
  * @param   string  $input    Localized number string
  * @param   array   $options  Options: number_format, locale, precision. See {@link setOptions()} for details.
  * @return  string  locale formatted number
  * @throws Zend_Locale_Exception
  */
 public static function toNumber($value, array $options = array())
 {
     // load class within method for speed
     // require_once 'Zend/Locale/Math.php';
     $value = Zend_Locale_Math::normalize($value);
     $value = Zend_Locale_Math::floatalize($value);
     $options = self::_checkOptions($options) + self::$_options;
     $options['locale'] = (string) $options['locale'];
     // Get correct signs for this locale
     $symbols = Zend_Locale_Data::getList($options['locale'], 'symbols');
     $oenc = iconv_get_encoding('internal_encoding');
     iconv_set_encoding('internal_encoding', 'UTF-8');
     // Get format
     $format = $options['number_format'];
     if ($format === null) {
         $format = Zend_Locale_Data::getContent($options['locale'], 'decimalnumber');
         $format = self::_seperateFormat($format, $value, $options['precision']);
         if ($options['precision'] !== null) {
             $value = Zend_Locale_Math::normalize(Zend_Locale_Math::round($value, $options['precision']));
         }
     } else {
         // seperate negative format pattern when available
         $format = self::_seperateFormat($format, $value, $options['precision']);
         if (strpos($format, '.')) {
             if (is_numeric($options['precision'])) {
                 $value = Zend_Locale_Math::round($value, $options['precision']);
             } else {
                 if (substr($format, iconv_strpos($format, '.') + 1, 3) == '###') {
                     $options['precision'] = null;
                 } else {
                     $options['precision'] = iconv_strlen(iconv_substr($format, iconv_strpos($format, '.') + 1, iconv_strrpos($format, '0') - iconv_strpos($format, '.')));
                     $format = iconv_substr($format, 0, iconv_strpos($format, '.') + 1) . '###' . iconv_substr($format, iconv_strrpos($format, '0') + 1);
                 }
             }
         } else {
             $value = Zend_Locale_Math::round($value, 0);
             $options['precision'] = 0;
         }
         $value = Zend_Locale_Math::normalize($value);
     }
     if (iconv_strpos($format, '0') === false) {
         iconv_set_encoding('internal_encoding', $oenc);
         // require_once 'Zend/Locale/Exception.php';
         throw new Zend_Locale_Exception('Wrong format... missing 0');
     }
     // get number parts
     $pos = iconv_strpos($value, '.');
     if ($pos !== false) {
         if ($options['precision'] === null) {
             $precstr = iconv_substr($value, $pos + 1);
         } else {
             $precstr = iconv_substr($value, $pos + 1, $options['precision']);
             if (iconv_strlen($precstr) < $options['precision']) {
                 $precstr = $precstr . str_pad("0", $options['precision'] - iconv_strlen($precstr), "0");
             }
         }
     } else {
         if ($options['precision'] > 0) {
             $precstr = str_pad("0", $options['precision'], "0");
         }
     }
     if ($options['precision'] === null) {
         if (isset($precstr)) {
             $options['precision'] = iconv_strlen($precstr);
         } else {
             $options['precision'] = 0;
         }
     }
     // get fraction and format lengths
     if (strpos($value, '.') !== false) {
         $number = substr((string) $value, 0, strpos($value, '.'));
     } else {
         $number = $value;
     }
     $prec = call_user_func(Zend_Locale_Math::$sub, $value, $number, $options['precision']);
     $prec = Zend_Locale_Math::floatalize($prec);
     $prec = Zend_Locale_Math::normalize($prec);
     if (iconv_strpos($prec, '-') !== false) {
         $prec = iconv_substr($prec, 1);
     }
     if ($prec == 0 and $options['precision'] > 0) {
         $prec = "0.0";
     }
     if ($options['precision'] + 2 > iconv_strlen($prec)) {
         $prec = str_pad((string) $prec, $options['precision'] + 2, "0", STR_PAD_RIGHT);
     }
     if (iconv_strpos($number, '-') !== false) {
         $number = iconv_substr($number, 1);
     }
     $group = iconv_strrpos($format, ',');
     $group2 = iconv_strpos($format, ',');
     $point = iconv_strpos($format, '0');
     // Add fraction
     $rest = "";
     if (iconv_strpos($format, '.')) {
         $rest = iconv_substr($format, iconv_strpos($format, '.') + 1);
         $length = iconv_strlen($rest);
         for ($x = 0; $x < $length; ++$x) {
             if ($rest[0] == '0' || $rest[0] == '#') {
                 $rest = iconv_substr($rest, 1);
             }
         }
         $format = iconv_substr($format, 0, iconv_strlen($format) - iconv_strlen($rest));
     }
     if ($options['precision'] == '0') {
         if (iconv_strrpos($format, '-') != 0) {
             $format = iconv_substr($format, 0, $point) . iconv_substr($format, iconv_strrpos($format, '#') + 2);
         } else {
             $format = iconv_substr($format, 0, $point);
         }
     } else {
         $format = iconv_substr($format, 0, $point) . $symbols['decimal'] . iconv_substr($prec, 2);
     }
     $format .= $rest;
     // Add seperation
     if ($group == 0) {
         // no seperation
         $format = $number . iconv_substr($format, $point);
     } else {
         if ($group == $group2) {
             // only 1 seperation
             $seperation = $point - $group;
             for ($x = iconv_strlen($number); $x > $seperation; $x -= $seperation) {
                 if (iconv_substr($number, 0, $x - $seperation) !== "") {
                     $number = iconv_substr($number, 0, $x - $seperation) . $symbols['group'] . iconv_substr($number, $x - $seperation);
                 }
             }
             $format = iconv_substr($format, 0, iconv_strpos($format, '#')) . $number . iconv_substr($format, $point);
         } else {
             // 2 seperations
             if (iconv_strlen($number) > $point - $group) {
                 $seperation = $point - $group;
                 $number = iconv_substr($number, 0, iconv_strlen($number) - $seperation) . $symbols['group'] . iconv_substr($number, iconv_strlen($number) - $seperation);
                 if (iconv_strlen($number) - 1 > $point - $group + 1) {
                     $seperation2 = $group - $group2 - 1;
                     for ($x = iconv_strlen($number) - $seperation2 - 2; $x > $seperation2; $x -= $seperation2) {
                         $number = iconv_substr($number, 0, $x - $seperation2) . $symbols['group'] . iconv_substr($number, $x - $seperation2);
                     }
                 }
             }
             $format = iconv_substr($format, 0, iconv_strpos($format, '#')) . $number . iconv_substr($format, $point);
         }
     }
     // set negative sign
     if (call_user_func(Zend_Locale_Math::$comp, $value, 0, $options['precision']) < 0) {
         if (iconv_strpos($format, '-') === false) {
             $format = $symbols['minus'] . $format;
         } else {
             $format = str_replace('-', $symbols['minus'], $format);
         }
     }
     iconv_set_encoding('internal_encoding', $oenc);
     return (string) $format;
 }
<?php

$a = str_repeat("/", 9000000);
var_dump(iconv_strrpos("a", "b", $a));