本文整理匯總了PHP中Zend\Stdlib\StringUtils::isValidUtf8方法的典型用法代碼示例。如果您正苦於以下問題:PHP StringUtils::isValidUtf8方法的具體用法?PHP StringUtils::isValidUtf8怎麽用?PHP StringUtils::isValidUtf8使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Zend\Stdlib\StringUtils
的用法示例。
在下文中一共展示了StringUtils::isValidUtf8方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: encodeText
/**
* Encode a text to match console encoding
*
* @param string $text
* @return string the encoding text
*/
public function encodeText($text)
{
if ($this->isUtf8()) {
if (StringUtils::isValidUtf8($text)) {
return $text;
}
return utf8_encode($text);
}
if (StringUtils::isValidUtf8($text)) {
return utf8_decode($text);
}
return $text;
}
示例2: render
/**
* Render a FIGlet text
*
* @param string $text Text to convert to a figlet text
* @param string $encoding Encoding of the input string
* @throws Exception\InvalidArgumentException When $text is not a string
* @throws Exception\UnexpectedValueException When $text it not properly encoded
* @return string
*/
public function render($text, $encoding = 'UTF-8')
{
if (!is_string($text)) {
throw new Exception\InvalidArgumentException('$text must be a string');
}
// Get the string wrapper supporting UTF-8 character encoding and the input encoding
$strWrapper = StringUtils::getWrapper($encoding, 'UTF-8');
// Convert $text to UTF-8 and check encoding
$text = $strWrapper->convert($text);
if (!StringUtils::isValidUtf8($text)) {
throw new Exception\UnexpectedValueException('$text is not encoded with ' . $encoding);
}
$strWrapper = StringUtils::getWrapper('UTF-8');
$this->output = '';
$this->outputLine = array();
$this->_clearLine();
$this->outlineLengthLimit = $this->outputWidth - 1;
$this->inCharLineLengthLimit = $this->outputWidth * 4 + 100;
$wordBreakMode = 0;
$lastCharWasEol = false;
$textLength = $strWrapper->strlen($text);
for ($charNum = 0; $charNum < $textLength; $charNum++) {
// Handle paragraphs
$char = $strWrapper->substr($text, $charNum, 1);
if ($char === "\n" && $this->handleParagraphs && !$lastCharWasEol) {
$nextChar = $strWrapper->substr($text, $charNum + 1, 1);
if (!$nextChar) {
$nextChar = null;
}
$char = ctype_space($nextChar) ? "\n" : ' ';
}
$lastCharWasEol = ctype_space($char) && $char !== "\t" && $char !== ' ';
if (ctype_space($char)) {
$char = $char === "\t" || $char === ' ' ? ' ' : "\n";
}
// Skip unprintable characters
$ordChar = $this->_uniOrd($char);
if ($ordChar > 0 && $ordChar < 32 && $char !== "\n" || $ordChar === 127) {
continue;
}
// Build the character
// Note: The following code is complex and thoroughly tested.
// Be careful when modifying!
do {
$charNotAdded = false;
if ($wordBreakMode === -1) {
if ($char === ' ') {
break;
} elseif ($char === "\n") {
$wordBreakMode = 0;
break;
}
$wordBreakMode = 0;
}
if ($char === "\n") {
$this->_appendLine();
$wordBreakMode = false;
} elseif ($this->_addChar($char)) {
if ($char !== ' ') {
$wordBreakMode = $wordBreakMode >= 2 ? 3 : 1;
} else {
$wordBreakMode = $wordBreakMode > 0 ? 2 : 0;
}
} elseif ($this->outlineLength === 0) {
for ($i = 0; $i < $this->charHeight; $i++) {
if ($this->rightToLeft === 1 && $this->outputWidth > 1) {
$offset = strlen($this->currentChar[$i]) - $this->outlineLengthLimit;
$this->_putString(substr($this->currentChar[$i], $offset));
} else {
$this->_putString($this->currentChar[$i]);
}
}
$wordBreakMode = -1;
} elseif ($char === ' ') {
if ($wordBreakMode === 2) {
$this->_splitLine();
} else {
$this->_appendLine();
}
$wordBreakMode = -1;
} else {
if ($wordBreakMode >= 2) {
$this->_splitLine();
} else {
$this->_appendLine();
}
$wordBreakMode = $wordBreakMode === 3 ? 1 : 0;
$charNotAdded = true;
}
} while ($charNotAdded);
}
//.........這裏部分代碼省略.........
示例3: testIsValidUtf8
/**
* @dataProvider getUtf8StringValidity
* @param string $str
* @param boolean $valid
*/
public function testIsValidUtf8($str, $valid)
{
$this->assertSame($valid, StringUtils::isValidUtf8($str));
}
示例4: ensureUtf8Encoded
/**
* @param string $subject
*
* @return string
*/
protected static function ensureUtf8Encoded($subject)
{
if (!StringUtils::isValidUtf8($subject)) {
$subject = utf8_encode($subject);
}
return $subject;
}