本文整理汇总了PHP中grapheme_substr函数的典型用法代码示例。如果您正苦于以下问题:PHP grapheme_substr函数的具体用法?PHP grapheme_substr怎么用?PHP grapheme_substr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了grapheme_substr函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testGrapheme_substr
/**
* @covers Patchwork\PHP\Override\Intl::grapheme_substr
* @covers Patchwork\PHP\Override\Intl::grapheme_substr_workaround62759
*/
function testGrapheme_substr()
{
$c = "déjà";
$this->assertSame("jà", grapheme_substr($c, 2));
$this->assertSame("jà", grapheme_substr($c, -2));
// The next 3 tests are disabled due to http://bugs.php.net/62759 and 55562
//$this->assertSame( "jà", grapheme_substr($c, -2, 3) );
//$this->assertSame( "", grapheme_substr($c, -1, 0) );
//$this->assertSame( false, grapheme_substr($c, 1, -4) );
$this->assertSame("j", grapheme_substr($c, -2, -1));
$this->assertSame("", grapheme_substr($c, -2, -2));
$this->assertSame(false, grapheme_substr($c, 5, 0));
$this->assertSame(false, grapheme_substr($c, -5, 0));
$this->assertSame("jà", p::grapheme_substr($c, 2));
$this->assertSame("jà", p::grapheme_substr($c, -2));
$this->assertSame("jà", p::grapheme_substr($c, -2, 3));
$this->assertSame("", p::grapheme_substr($c, -1, 0));
$this->assertSame(false, p::grapheme_substr($c, 1, -4));
$this->assertSame("j", p::grapheme_substr($c, -2, -1));
$this->assertSame("", p::grapheme_substr($c, -2, -2));
$this->assertSame(false, p::grapheme_substr($c, 5, 0));
$this->assertSame(false, p::grapheme_substr($c, -5, 0));
$this->assertSame("jà", p::grapheme_substr_workaround62759($c, 2, 2147483647));
$this->assertSame("jà", p::grapheme_substr_workaround62759($c, -2, 2147483647));
$this->assertSame("jà", p::grapheme_substr_workaround62759($c, -2, 3));
$this->assertSame("", p::grapheme_substr_workaround62759($c, -1, 0));
$this->assertSame(false, p::grapheme_substr_workaround62759($c, 1, -4));
$this->assertSame("j", p::grapheme_substr_workaround62759($c, -2, -1));
$this->assertSame("", p::grapheme_substr_workaround62759($c, -2, -2));
$this->assertSame(false, p::grapheme_substr_workaround62759($c, 5, 0));
$this->assertSame(false, p::grapheme_substr_workaround62759($c, -5, 0));
}
示例2: substr
/**
* Realiable unicode substring.
*
* @param string $input
* @return string
*/
public static function substr($input, $start = 0, $length = null)
{
if (is_null($length)) {
return grapheme_substr($input, $start);
}
return grapheme_substr($input, $start, $length);
}
示例3: substr
/**
* Returns the portion of string specified by the start and length parameters
*
* @param string $str
* @param int $offset
* @param int|null $length
* @return string|false
*/
public function substr($str, $offset = 0, $length = null)
{
// Due fix of PHP #62759 The third argument returns an empty string if is 0 or null.
if ($length !== null) {
return grapheme_substr($str, $offset, $length);
}
return grapheme_substr($str, $offset);
}
示例4: excerpt
public function excerpt($field, $length = 200)
{
$text = strip_tags($this->{$field});
if (grapheme_strlen($text) > $length) {
return grapheme_substr($text, 0, $length) . '...';
} else {
return $text;
}
}
示例5: utf8Substr
public static function utf8Substr($string, $start, $length = NULL)
{
if (function_exists('grapheme_substr')) {
return grapheme_substr($string, $start, $length);
} elseif (function_exists('mb_sustr')) {
$length = mb_substr($string, $start, $length, 'UTF-8');
} else {
$length = substr($string, $start, $length);
}
}
示例6: summarize
public static function summarize($aText, $aLength, $aSuffix = '...')
{
$text = $aText;
if (grapheme_strlen($aText) > 0) {
if (grapheme_strlen($text) > $aLength) {
$text = trim($text);
$text = grapheme_substr($text, 0, $aLength);
if ($aLength > 0) {
//trim the end at a word boundary
$text = strrev($text);
if (preg_match('/(?:\\s(\\S))|\\./', $text, $matches, PREG_OFFSET_CAPTURE) && count($matches) > 1) {
$newEnd = $matches[1][1];
if ($matches[1][0] == '.') {
$newEnd++;
}
$text = grapheme_substr($text, $newEnd);
}
$text = strrev($text) . $aSuffix;
}
}
}
return $text;
}
示例7: var_dump
<?php
var_dump(grapheme_substr('Iñtërnâtiônàlizætiøn', 10, -2));
示例8: countDecimalDigits
/**
* Count the number of decimals that $number contains
*
* @param string $number
* @param string $separatorSymbol
* @param string $currencySymbol
* @return int
*/
protected function countDecimalDigits($number, $separatorSymbol, $currencySymbol)
{
// Remove currency symbol (if any) from string
$number = str_replace($currencySymbol, '', $number);
// Retrieve last occurence of monetary separator symbol
$lastOccurence = grapheme_strrpos($number, $separatorSymbol);
if ($lastOccurence === false) {
return 0;
}
$decimals = grapheme_substr($number, $lastOccurence + 1);
return preg_match_all(sprintf('#%s#%s', $this->getRegexComponent(self::REGEX_NUMBERS), $this->getRegexComponent(self::REGEX_FLAGS)), $decimals);
}
示例9: substr
/**
* Here used as a multibyte enabled equivalent of `substr()`.
*
* @link http://php.net/manual/en/function.grapheme-substr.php
* @param string $string
* @param integer $start
* @param integer $length
* @return string|boolean
*/
public function substr($string, $start, $length)
{
return grapheme_substr($string, $start, $length);
}
示例10: str_pad
static function str_pad($s, $len, $pad = ' ', $type = STR_PAD_RIGHT)
{
$slen = grapheme_strlen($s);
if ($len <= $slen) {
return $s;
}
$padlen = grapheme_strlen($pad);
$freelen = $len - $slen;
$len = $freelen % $padlen;
if (STR_PAD_RIGHT == $type) {
return $s . str_repeat($pad, $freelen / $padlen) . ($len ? grapheme_substr($pad, 0, $len) : '');
}
if (STR_PAD_LEFT == $type) {
return str_repeat($pad, $freelen / $padlen) . ($len ? grapheme_substr($pad, 0, $len) : '') . $s;
}
if (STR_PAD_BOTH == $type) {
$freelen /= 2;
$type = ceil($freelen);
$len = $type % $padlen;
$s .= str_repeat($pad, $type / $padlen) . ($len ? grapheme_substr($pad, 0, $len) : '');
$type = floor($freelen);
$len = $type % $padlen;
return str_repeat($pad, $type / $padlen) . ($len ? grapheme_substr($pad, 0, $len) : '') . $s;
}
user_error(__METHOD__ . '(): Padding type has to be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH', E_USER_WARNING);
}
示例11: ut_main
//.........这里部分代码省略.........
$res_str .= " from {$test['2']}";
$result = grapheme_strrpos($test[0], $test[1], $test[2]);
}
$res_str .= " = ";
if ($result === false) {
$res_str .= 'false';
} else {
$res_str .= $result;
}
$res_str .= " == " . $test[count($test) - 1] . check_result($result, $test[count($test) - 1]) . "\n";
}
//=====================================================================================
$res_str .= "\n" . 'function grapheme_strripos($haystack, $needle, $offset = 0) {}' . "\n\n";
$tests = array(array("ao" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "O", "o", 2, 6), array($char_o_diaeresis_nfd . $char_a_ring_nfd . "a" . $char_A_ring_nfd . "bc", $char_a_ring_nfd, 2, 3), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "O", "o", 5), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, "O", "false"), array("a" . $char_a_ring_nfd . "bc" . $char_O_diaeresis_nfd, $char_o_diaeresis_nfd, 4), array($char_o_diaeresis_nfd . "a" . $char_a_ring_nfd . "bc", $char_A_ring_nfd, 2), array("a" . $char_A_ring_nfd . "bc", $char_a_ring_nfd, 1), array("Abc", $char_a_ring_nfd, "false"), array($char_a_ring_nfd . "bc", "A", "false"), array("abc", "D", "false"), array("abC", "c", 2), array("abc", "B", 1), array("Abc", "a", 0), array("abc", "A", 0, 0), array("Abc", "a", 1, "false"), array("ababc", "A", 1, 2), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", "oP", 5), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", "opQ", 5), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, "abc", "false"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "bC" . $char_o_diaeresis_nfd, $char_O_diaeresis_nfd . "bc" . $char_o_diaeresis_nfd, 4), array($char_o_diaeresis_nfd . "a" . $char_a_ring_nfd . "Bc", $char_A_ring_nfd . "bc", 2), array("a" . $char_a_ring_nfd . "BC", $char_a_ring_nfd . "bc", 1), array("abc", $char_a_ring_nfd . "BC", "false"), array($char_a_ring_nfd . "BC", "aBCdefg", "false"), array("aBC", "Defghijklmnopq", "false"), array("abC", "Ab", 0), array("aBC", "bc", 1), array("abC", "Abc", 0), array("abC", "aBcd", "false"), array("ABc", "ab", 0, 0), array("aBc", "abC", 0, 0), array("abc", "aBc", 1, "false"), array("ABabc", "AB", 1, 2), array("abaBc", "aBc", 1, 2), array("ao" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "o" . $char_A_ring_nfd . "bC", "O" . $char_a_ring_nfd . "bC", 2, 6), array($char_o_diaeresis_nfd . $char_a_ring_nfd . "a" . $char_A_ring_nfd . "bC" . $char_a_ring_nfd . "def", $char_a_ring_nfd . "Bc" . $char_a_ring_nfd, 2, 3));
foreach ($tests as $test) {
$arg1 = urlencode($test[1]);
$arg0 = urlencode($test[0]);
$res_str .= "find \"{$arg1}\" in \"{$arg0}\" - grapheme_strripos";
if (3 == count($test)) {
$result = grapheme_strripos($test[0], $test[1]);
} else {
$res_str .= " from {$test['2']}";
$result = grapheme_strripos($test[0], $test[1], $test[2]);
}
$res_str .= " = ";
if ($result === false) {
$res_str .= 'false';
} else {
$res_str .= $result;
}
$res_str .= " == " . $test[count($test) - 1] . check_result($result, $test[count($test) - 1]) . "\n";
}
//=====================================================================================
$res_str .= "\n" . 'function grapheme_substr($string, $start, $length = -1) {}' . "\n\n";
$tests = array(array("abc", 3, "false"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, 5, "false"), array("ao" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "O", 2, $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "O"), array($char_o_diaeresis_nfd . $char_a_ring_nfd . "a" . $char_A_ring_nfd . "bc", 2, "a" . $char_A_ring_nfd . "bc"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "O", 5, "O"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, 5, "false"), array("a" . $char_a_ring_nfd . "bc" . $char_O_diaeresis_nfd, 4, $char_O_diaeresis_nfd), array($char_o_diaeresis_nfd . "a" . $char_a_ring_nfd . "bc", 2, $char_a_ring_nfd . "bc"), array("a" . $char_A_ring_nfd . "bc", 1, $char_A_ring_nfd . "bc"), array("Abc", -5, "false"), array($char_a_ring_nfd . "bc", 3, "false"), array("abc", 4, "false"), array("abC", 2, "C"), array("abc", 1, "bc"), array("Abc", 1, 1, "b"), array("abc", 0, 2, "ab"), array("Abc", -4, 1, "false"), array("ababc", 1, 2, "ba"), array("ababc", 0, 10, "ababc"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, 10, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 5, "Opq"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 5, -1, "Op"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 5, -2, "O"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 5, -3, ""), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 5, -4, "false"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, -1, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Op"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, -2, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "O"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, -3, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, -4, "a" . $char_a_ring_nfd . "bc"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, -5, "a" . $char_a_ring_nfd . "b"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, -6, "a" . $char_a_ring_nfd), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, -7, "a"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, -8, ""), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, -9, "false"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -7, $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -6, "bc" . $char_o_diaeresis_nfd . "Opq"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -5, "c" . $char_o_diaeresis_nfd . "Opq"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -4, $char_o_diaeresis_nfd . "Opq"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -3, "Opq"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -2, "pq"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -1, "q"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -999, "false"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, 8, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, 7, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Op"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, 6, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "O"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, 5, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, 4, "a" . $char_a_ring_nfd . "bc"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, 3, "a" . $char_a_ring_nfd . "b"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, 2, "a" . $char_a_ring_nfd), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, 1, "a"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, 0, ""), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, -999, "false"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, -1, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Op"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, -2, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "O"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, -3, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, -4, "a" . $char_a_ring_nfd . "bc"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, -5, "a" . $char_a_ring_nfd . "b"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, -6, "a" . $char_a_ring_nfd), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, -7, "a"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, -8, ""), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, -9, "false"));
foreach ($tests as $test) {
$arg0 = urlencode($test[0]);
$res_str .= "substring of \"{$arg0}\" from \"{$test['1']}\" - grapheme_substr";
if (3 == count($test)) {
$result = grapheme_substr($test[0], $test[1]);
} else {
$res_str .= " with length {$test['2']}";
$result = grapheme_substr($test[0], $test[1], $test[2]);
}
$res_str .= " = ";
if ($result === false) {
$res_str .= 'false';
} else {
$res_str .= urlencode($result);
}
$res_str .= " == " . urlencode($test[count($test) - 1]) . check_result($result, $test[count($test) - 1]) . "\n";
}
//=====================================================================================
$res_str .= "\n" . 'function grapheme_strstr($haystack, $needle, $before_needle = FALSE) {}' . "\n\n";
$tests = array(array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "o", "o", "o"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, "o", "false"), array("a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, $char_o_diaeresis_nfd, $char_o_diaeresis_nfd), array($char_o_diaeresis_nfd . "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd, $char_a_ring_nfd . "bc"), array("a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd, $char_a_ring_nfd . "bc"), array("abc", $char_a_ring_nfd, "false"), array($char_a_ring_nfd . "bc", "a", "false"), array("abc", "d", "false"), array("abc", "c", "c"), array("abc", "b", "bc"), array("abc", "a", "abc"), array("abc", "ab", "abc"), array("abc", "abc", "abc"), array("abc", "bc", "bc"), array("abc", "a", FALSE, "abc"), array("abc", "a", TRUE, ""), array("abc", "b", TRUE, "a"), array("abc", "c", TRUE, "ab"), array("ababc", "bab", TRUE, "a"), array("ababc", "abc", TRUE, "ab"), array("ababc", "abc", FALSE, "abc"), array("ab" . $char_a_ring_nfd . "c", "d", "false"), array("bc" . $char_a_ring_nfd . "a", "a", "a"), array("a" . $char_a_ring_nfd . "bc", "b", "bc"), array($char_a_ring_nfd . "bc", "a", "false"), array($char_a_ring_nfd . "abc", "ab", "abc"), array("abc" . $char_a_ring_nfd, "abc", "abc" . $char_a_ring_nfd), array("a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd . "bc", $char_a_ring_nfd . "bc"), array("a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd, FALSE, $char_a_ring_nfd . "bc"), array("a" . $char_a_ring_nfd . "bc", "a", TRUE, ""), array($char_a_ring_nfd . "abc", "b", TRUE, $char_a_ring_nfd . "a"), array("ab" . $char_a_ring_nfd . "c", "c", TRUE, "ab" . $char_a_ring_nfd), array("aba" . $char_a_ring_nfd . "bc", "ba" . $char_a_ring_nfd . "b", TRUE, "a"), array("ababc" . $char_a_ring_nfd, "abc" . $char_a_ring_nfd, TRUE, "ab"), array("abab" . $char_a_ring_nfd . "c", "ab" . $char_a_ring_nfd . "c", FALSE, "ab" . $char_a_ring_nfd . "c"));
foreach ($tests as $test) {
$arg1 = urlencode($test[1]);
$arg0 = urlencode($test[0]);
$res_str .= "find \"{$arg1}\" in \"{$arg0}\" - grapheme_strstr";
if (3 == count($test)) {
$result = grapheme_strstr($test[0], $test[1]);
} else {
$res_str .= " before flag is " . ($test[2] ? "TRUE" : "FALSE");
$result = grapheme_strstr($test[0], $test[1], $test[2]);
}
$res_str .= " = ";
示例12: substr
/**
* Returns part of a string.
*
* @param string $string The string to be converted.
* @param integer $start The part's start position, zero based.
* @param integer $length The part's length.
* @param string $charset The charset to use when calculating the part's
* position and length, defaults to current
* charset.
*
* @return string The string's part.
*/
public static function substr($string, $start, $length = null, $charset = 'UTF-8')
{
if (is_null($length)) {
$length = self::length($string, $charset) - $start;
}
if ($length === 0) {
return '';
}
$error = false;
/* Try mbstring. */
if (Horde_Util::extensionExists('mbstring')) {
$ret = @mb_substr($string, $start, $length, self::_mbstringCharset($charset));
/* mb_substr() returns empty string on failure. */
if (strlen($ret)) {
return $ret;
}
$error = true;
}
/* Try iconv. */
if (Horde_Util::extensionExists('iconv')) {
$ret = @iconv_substr($string, $start, $length, $charset);
/* iconv_substr() returns false on failure. */
if ($ret !== false) {
return $ret;
}
$error = true;
}
/* Try intl. */
if (Horde_Util::extensionExists('intl')) {
$ret = self::convertCharset(@grapheme_substr(self::convertCharset($string, $charset, 'UTF-8'), $start, $length), 'UTF-8', $charset);
/* grapheme_substr() returns false on failure. */
if ($ret !== false) {
return $ret;
}
$error = true;
}
return $error ? '' : substr($string, $start, $length);
}
示例13: substr
/**
* Returns a substring from a string.
*
* As a special case, the method returns an empty string if the starting position is equal to the string's length
* or if the substring's length, if specified, is `0`.
*
* @param string $string The string to be looked into.
* @param int $startPos The position of the substring's first character.
* @param int $length **OPTIONAL. Default is** *as many characters as the starting character is followed by*. The
* length of the substring.
*
* @return string The substring.
*/
public static function substr($string, $startPos, $length = null)
{
assert('is_cstring($string) && is_int($startPos) && (!isset($length) || is_int($length))', vs(isset($this), get_defined_vars()));
assert('(0 <= $startPos && $startPos < self::length($string)) || ' . '($startPos == self::length($string) && (!isset($length) || $length == 0))', vs(isset($this), get_defined_vars()));
assert('!isset($length) || ($length >= 0 && $startPos + $length <= self::length($string))', vs(isset($this), get_defined_vars()));
$res;
if (!isset($length)) {
$res = grapheme_substr($string, $startPos);
} else {
$res = grapheme_substr($string, $startPos, $length);
}
return is_cstring($res) ? $res : "";
}
示例14: substr
/**
* Returns the portion of string specified by the start and length parameters
*
* @param string $str
* @param int $offset
* @param int|null $length
* @param string $encoding
* @return string|false
*/
public function substr($str, $offset = 0, $length = null)
{
return grapheme_substr($str, $offset, $length);
}
示例15: valuesProvider
/**
* Generate dataset.
*
* Formats:
* - (positive and negative) currency amounts with their own currency symbol
* - (positive and negative) currency amounts with ISO currency symbol
* - (positive and negative) numbers (without currency symbol)
* - (positive and negative) numbers expressed in scientific notation (without currency symbol)
*
* @return array
*/
public function valuesProvider()
{
$data = [];
$values = [0, 0.1, 0.01, 1000, 1234.61, 12345678.9];
$values = array_unique(array_merge($values, array_map(function ($i) {
return -$i;
}, $values)));
foreach ($this->locales as $locale) {
$formatter = \NumberFormatter::create($locale, \NumberFormatter::CURRENCY);
$currencySymbol = $formatter->getSymbol(\NumberFormatter::CURRENCY_SYMBOL);
$isoSymbol = $formatter->getTextAttribute(\NumberFormatter::CURRENCY_CODE);
$groupSep = $formatter->getSymbol(\NumberFormatter::MONETARY_GROUPING_SEPARATOR_SYMBOL);
$numDecimals = $formatter->getAttribute(\NumberFormatter::FRACTION_DIGITS);
$posPre = $formatter->getTextAttribute(\NumberFormatter::POSITIVE_PREFIX);
$negPre = $formatter->getTextAttribute(\NumberFormatter::NEGATIVE_PREFIX);
$posSuf = $formatter->getTextAttribute(\NumberFormatter::POSITIVE_SUFFIX);
$negSuf = $formatter->getTextAttribute(\NumberFormatter::NEGATIVE_SUFFIX);
$exponantiatior = \NumberFormatter::create($locale, \NumberFormatter::SCIENTIFIC);
foreach ($values as $value) {
// Restore currency symbol
$formatter->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, $currencySymbol);
if (is_float($value)) {
// If value is float and current currency does not have cents, jump it
if ($numDecimals === 0) {
continue;
}
// Create a currency with less decimal places then required (w/ currency symbol)
$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $numDecimals - 1);
$currency = preg_replace('/^[\\xC2\\xA0\\s]+|[\\xC2\\xA0\\s]+$/u', '', $formatter->format($value));
// echo $currency . PHP_EOL;
$data[] = [$locale, true, true, $currency, $currency];
// Not filtered
$data[] = [$locale, true, false, $currency, $currency];
// Not filtered
$data[] = [$locale, false, false, (double) sprintf('%.' . ($numDecimals - 1) . 'f', $value), $currency];
// Filtered
$data[] = [$locale, false, true, (double) sprintf('%.' . ($numDecimals - 1) . 'f', $value), $currency];
// Filtered
// Create a currency with less decimal places then required (w/o currency symbol)
$currency = preg_replace('#' . preg_quote($currencySymbol) . '#u', '', $currency);
$currency = preg_replace('/^[\\xC2\\xA0\\s]+|[\\xC2\\xA0\\s]+$/u', '', $currency);
// echo $currency . PHP_EOL;
$data[] = [$locale, true, true, $currency, $currency];
// Not filtered
$data[] = [$locale, true, false, $currency, $currency];
// Not filtered
$data[] = [$locale, false, false, (double) sprintf('%.' . ($numDecimals - 1) . 'f', $value), $currency];
// Filtered
$data[] = [$locale, false, true, $currency, $currency];
// Not filtered
// Create a currency with more decimal places then required (w/ currency symbol)
$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $numDecimals + 1);
$currency = preg_replace('/^[\\xC2\\xA0\\s]+|[\\xC2\\xA0\\s]+$/u', '', $formatter->format($value));
// echo $currency . PHP_EOL;
$data[] = [$locale, true, true, $currency, $currency];
// Not filtered
$data[] = [$locale, true, false, $currency, $currency];
// Not filtered
$data[] = [$locale, false, false, (double) sprintf('%.' . ($numDecimals + 1) . 'f', $value), $currency];
// Filtered
$data[] = [$locale, false, true, (double) sprintf('%.' . ($numDecimals + 1) . 'f', $value), $currency];
// Filtered
// Create a currency with more decimal places then required (w/o currency symbol)
$currency = preg_replace('#' . preg_quote($currencySymbol) . '#u', '', $currency);
$currency = preg_replace('/^[\\xC2\\xA0\\s]+|[\\xC2\\xA0\\s]+$/u', '', $currency);
// echo $currency . PHP_EOL;
$data[] = [$locale, true, true, $currency, $currency];
// Not filtered
$data[] = [$locale, true, false, $currency, $currency];
// Not filtered
$data[] = [$locale, false, false, (double) sprintf('%.' . ($numDecimals + 1) . 'f', $value), $currency];
// Filtered
$data[] = [$locale, false, true, $currency, $currency];
// Not filtered
}
// Restore correct number of maximum decimal places
$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $numDecimals);
// Create completely formatted currency value (w/ currency symbol)
$currency = $formatter->formatCurrency($value, $isoSymbol);
// echo $currency . PHP_EOL;
$data[] = [$locale, true, true, $value, $currency];
// Filtered
// Create currency value with letters inside
$randomPos = rand(0, grapheme_strlen($currency) - 1);
$currency = grapheme_substr($currency, 0, $randomPos) . 'X' . grapheme_substr($currency, $randomPos);
// echo $currency . PHP_EOL;
$daa[] = [$locale, true, true, $currency, $currency];
// Not filtered
// Create currency value (w/ currency symbol) (w/o group separators)
//.........这里部分代码省略.........