Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

103 lines (93 sloc) 2.83 kb
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Stdlib
*/
namespace Zend\Stdlib\StringWrapper;
/**
* @category Zend
* @package Zend_Stdlib
* @subpackage StringWrapper
*/
class MbString extends AbstractStringWrapper
{
/**
* List of supported character sets (upper case)
*
* @var string[]
* @link http://php.net/manual/mbstring.supported-encodings.php
*/
protected $encodings = array();
/**
* Constructor
*
* @throws Exception\ExtensionNotLoadedException
*/
public function __construct()
{
if (!extension_loaded('mbstring')) {
throw new Exception\ExtensionNotLoadedException(
'PHP extension "mbstring" is required for this wrapper'
);
}
$this->encodings = array_map('strtoupper', mb_list_encodings());
// FIXME: Converting € (UTF-8) to ISO-8859-16 gives a wrong result
$indexIso885916 = array_search('ISO-8859-16', $this->encodings, true);
if ($indexIso885916 !== false) {
unset($this->encodings[$indexIso885916]);
}
}
/**
* Returns the length of the given string
*
* @param string $str
* @param string $encoding
* @return int|false
*/
public function strlen($str, $encoding = 'UTF-8')
{
return mb_strlen($str, $encoding);
}
/**
* 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, $encoding = 'UTF-8')
{
return mb_substr($str, $offset, $length, $encoding);
}
/**
* Find the position of the first occurrence of a substring in a string
*
* @param string $haystack
* @param string $needle
* @param int $offset
* @param string $encoding
* @return int|false
*/
public function strpos($haystack, $needle, $offset = 0, $encoding = 'UTF-8')
{
return mb_strpos($haystack, $needle, $offset, $encoding);
}
/**
* Convert a string from one character encoding to another
*
* @param string $str
* @param string $toEncoding
* @param string $fromEncoding
* @return string|false
*/
public function convert($str, $toEncoding, $fromEncoding = 'UTF-8')
{
return mb_convert_encoding($str, $toEncoding, $fromEncoding);
}
}
Jump to Line
Something went wrong with that request. Please try again.