本文整理汇总了PHP中mb_check_encoding函数的典型用法代码示例。如果您正苦于以下问题:PHP mb_check_encoding函数的具体用法?PHP mb_check_encoding怎么用?PHP mb_check_encoding使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了mb_check_encoding函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: validation_default
private static function validation_default($val, $min_len = 0, $max_len = 60, $regex_opt = '')
{
if (!is_string($val)) {
// validation_exit('Invalid type', gettype($val));
return "";
}
if (strlen($val) < $min_len || strlen($val) > $max_len) {
validation_exit('Too long value', strlen($val));
return "";
}
// Only UTF-8 is supported.
// WARNING: This code assumes UTF-8 only script.
if (ini_get('default_charset') != 'UTF-8') {
// validation_exit('Only UTF-8 is supported', $val);
return "";
}
if (!mb_check_encoding($val, 'UTF-8')) {
// validation_exit('Invalid encoding', $val);
return "";
}
// Allow only alpha numeric and UTF-8.
// UTF-8 encoding:
// 0xxxxxxx
// 110yyyyx + 10xxxxxx
// 1110yyyy + 10yxxxxx + 10xxxxxx
// 11110yyy + 10yyxxxx + 10xxxxxx + 10xxxxxx
// Since validity of UTF-8 encoding is checked, simply allow \x80-\xFF.
if (!mb_ereg('\\A[0-9A-Za-z\\x80-\\xFF' . $regex_opt . ']*\\z', $val)) {
// validation_exit('Invalid char', $val);
}
return $val;
}
示例2: load
public function load($url = null)
{
if (is_null($url) && is_null($this->url)) {
throw new Exception('You must set url to load');
}
if (empty($url)) {
$url = $this->url;
}
curl_setopt($this->curl, CURLOPT_URL, $url);
$response = curl_exec($this->curl);
if (curl_errno($this->curl) !== 0) {
throw new \Exception('cURL error: ' . curl_error($this->curl));
}
$httpCode = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
if ($httpCode != 200) {
throw new \Exception('cURL request returned HTTP code ' . $httpCode);
}
$headersLength = curl_getinfo($this->curl, CURLINFO_HEADER_SIZE);
$headers = substr($response, 0, $headersLength);
$response = substr($response, $headersLength);
$this->url = curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL);
$this->host = parse_url($this->url, PHP_URL_HOST);
$headers = preg_split('/^\\s*$/m', trim($headers));
// get last headers, after all redirects
$this->headers = end($headers);
$this->html = $response;
$this->html = mb_check_encoding($this->html, 'UTF-8') ? $this->html : utf8_encode($this->html);
}
示例3: checkUtf8Encoding
/**
* Check for invalid UTF8 encoding and invalid byte .
*
* @param string $string Your string.
*
* @return boolean
*/
public static function checkUtf8Encoding($string)
{
if (!mb_check_encoding($string, 'UTF-8') || !$string == mb_convert_encoding(mb_convert_encoding($string, 'UTF-32', 'UTF-8'), 'UTF-8', 'UTF-32')) {
return false;
}
return true;
}
示例4: curl
/**
* Perform a cURL request
* @param string $url
*/
protected function curl($url)
{
if ($this->debug) {
echo 'cURL request: ' . $url . "\n";
}
$ch = curl_init($url);
curl_setopt_array($ch, array(CURLOPT_SSL_VERIFYPEER => false, CURLOPT_HEADER => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => $this->curlFollowLocation, CURLOPT_MAXREDIRS => $this->curlMaxRedirects, CURLOPT_TIMEOUT => $this->curlTimeout, CURLOPT_USERAGENT => $this->curlUserAgent));
$response = curl_exec($ch);
if (curl_errno($ch) !== 0) {
throw new WappalyzerException('cURL error: ' . curl_error($ch));
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode != 200) {
throw new WappalyzerException('cURL request returned HTTP code ' . $httpCode);
}
$result = new stdClass();
$result->url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$result->host = parse_url($result->url, PHP_URL_HOST);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$result->html = substr($response, $headerSize);
$result->html = mb_check_encoding($result->html, 'UTF-8') ? $result->html : utf8_encode($result->html);
$headers = trim(substr($response, 0, $headerSize));
$headers = preg_split('/^\\s*$/m', $headers);
$headers = end($headers);
$lines = array_slice(explode("\r\n", $headers), 1);
foreach ($lines as $line) {
if (strpos(trim($line), ': ') !== false) {
list($key, $value) = explode(': ', $line);
$result->headers[strtolower($key)] = $value;
}
}
return $result;
}
示例5: convertEncoding
/**
* Ensures the encoding of the passed string is set to UTF-8.
*
* @param string $str
* @return string utf-8 string
*/
protected function convertEncoding($str)
{
if (!mb_check_encoding($str, 'UTF-8')) {
return mb_convert_encoding($str, 'UTF-8', 'ISO-8859-1');
}
return $str;
}
示例6: prepare_content
protected function prepare_content($content)
{
$result = $content;
if (empty($result)) {
return '';
}
$encoding = null;
$xml_error = new libxml_errors_mgr();
$dom = new DOMDocument();
$dom->validateOnParse = false;
$dom->strictErrorChecking = false;
if ($dom->loadHTML($content)) {
$encoding = $dom->xmlEncoding;
}
if (empty($encoding)) {
$encoding = mb_detect_encoding($content, 'auto', true);
}
if (!empty($encoding) && !mb_check_encoding($content, 'UTF-8')) {
$result = mb_convert_encoding($content, 'UTF-8', $encoding);
}
// See if we can strip off body tag and anything outside of it.
foreach (array('body', 'html') as $tagname) {
$regex = str_replace('##', $tagname, "/<##[^>]*>(.+)<\\/##>/is");
if (preg_match($regex, $result, $matches)) {
$result = $matches[1];
break;
}
}
return $result;
}
示例7: sendText
/**
* Prepare new text message.
*
* If $unicode is not provided we will try to detect the
* message type. Otherwise set to TRUE if you require
* unicode characters.
*/
public function sendText($to, $from, $message, $unicode = null)
{
// Making sure strings are UTF-8 encoded
if (!is_numeric($from) && !mb_check_encoding($from, 'UTF-8')) {
trigger_error('$from needs to be a valid UTF-8 encoded string');
return false;
}
if (!mb_check_encoding($message, 'UTF-8')) {
trigger_error('$message needs to be a valid UTF-8 encoded string');
return false;
}
if ($unicode === null) {
$containsUnicode = max(array_map('ord', str_split($message))) > 127;
} else {
$containsUnicode = (bool) $unicode;
}
// Make sure $from is valid
$from = $this->validateOriginator($from);
// URL Encode
$sFrom = urlencode($from);
$sMessage = urlencode($message);
// Send away!
$hPost = ['from' => $sFrom, 'to' => $to, 'message' => $sMessage, 'type' => $containsUnicode ? 'unicode' : 'text'];
return $this->sendSmsRequest($hPost);
}
示例8: __construct
/**
* Class constructor.
*
* @param mixed $string
* @param string $encoding
* @throws \InvalidArgumentException
*/
public function __construct($string, $encoding = self::ENCODING)
{
if (is_null($string)) {
$string = 'null';
} elseif (is_bool($string)) {
$string = $string ? 'true' : 'false';
} elseif (is_int($string) || is_float($string)) {
$string = (string) $string;
} elseif (is_object($string)) {
if (!method_exists($string, '__toString')) {
throw new \InvalidArgumentException(sprintf('Object of class %s cannot be converted to String', get_class($string)));
}
$string = (string) $string;
} elseif (!is_string($string)) {
throw new \InvalidArgumentException('Cannot convert a variable of type ' . gettype($string) . ' to String');
}
if (!self::checkEncoding($encoding)) {
throw new \InvalidArgumentException('Unsupported encoding: ' . $encoding);
}
if (!mb_check_encoding($string, $encoding)) {
throw new \InvalidArgumentException('String is not encoded in ' . $encoding);
}
if ($encoding != self::ENCODING) {
$string = mb_convert_encoding($string, self::ENCODING, $encoding);
}
$string = \Normalizer::normalize($string);
$this->string = $string;
$this->length = mb_strlen($string, self::ENCODING);
}
示例9: testParse
/**
* @param string|\Closure $input
* @param string|null $filename
* @param string|null $title
* @param (string|string[]|float)[][][] $jsonable
* @param (string|string[])[] $metadata
* @param string[] $logLevels
* @dataProvider dictionaryProvider
*/
public function testParse($input, string $filename = null, string $title = null, array $jsonable = null, array $metadata = null, array $logLevels = [])
{
$parser = new GenericDictionaryParser();
$parser->setLogger($this);
if ($input instanceof \Closure) {
$archive = $input();
$file = new \SplFileInfo($archive->filename);
$archive->close();
$dictionary = $parser->parse($file, $filename, $title);
} else {
$dictionary = $parser->parse($this->generateTempFileObject(mb_check_encoding($input, 'UTF-8') ? $this->stripIndents($input) : $input), $filename, $title);
}
array_walk_recursive($jsonable, (function (&$field) {
if (is_string($field)) {
$field = $this->stripIndents($field);
}
})->bindTo($this));
$this->assertEquals($jsonable, $dictionary->getWords());
array_walk_recursive($metadata, (function (string &$field) {
if (is_string($field)) {
$field = $this->stripIndents($field);
}
})->bindTo($this));
$this->assertEquals($metadata, $dictionary->getMetadata());
$this->assertEquals($logLevels, $this->logLevels);
}
示例10: is_utf8
/**
* @param $identifier
*
* @return bool
*/
public static function is_utf8($identifier)
{
if (mb_check_encoding($identifier, 'UTF-8')) {
return true;
}
return false;
}
示例11: isValidUTF8
/**
* Ckecks a string for valid UTF8 encoding
*
* @param $string
*
* @return bool
*/
public static function isValidUTF8($string)
{
if (function_exists("mb_check_encoding")) {
return mb_check_encoding($string, "UTF-8") ? TRUE : FALSE;
}
return preg_match("//u", $string) ? TRUE : FALSE;
}
示例12: json_encode_string
function json_encode_string($in_str)
{
if (mb_check_encoding($in_str) != "UTF-8") {
$in_str = mb_convert_encoding($in_str, "UTF-8");
}
return str_replace('"', '\\"', $in_str);
}
示例13: coins
function coins($row)
{
// fmt_info (type)
$fmt = "info:ofi/fmt:kev:mtx:";
// 'dissertation' is compatible with the 1.0 spec, but not the 0.1 spec
if (!empty($row['thesis'])) {
$fmt .= "dissertation";
} elseif (preg_match("/Journal/", $row['type'])) {
$fmt .= "journal";
} elseif (preg_match("/Patent/", $row['type'])) {
$fmt .= "patent";
} elseif (preg_match("/Book/", $row['type'])) {
$fmt .= "book";
} else {
$fmt .= "dc";
}
$co = contextObject($row);
$coins = "ctx_ver=Z39.88-2004" . "&rft_val_fmt=" . urlencode($fmt);
foreach ($co as $coKey => $coValue) {
// 'urlencode()' differs from 'rawurlencode() (i.e., RFC1738 encoding)
// in that spaces are encoded as plus (+) signs
$coKey = preg_replace("/au[0-9]*/i", "au", $coKey);
// While COinS does not specify encoding, most javascript tools assume that it is UTF-8
// TODO: use function 'detectCharacterEncoding()' instead of the 'mb_*()' functions?
// if (($contentTypeCharset == "ISO-8859-1") AND (detectCharacterEncoding($coValue) != "UTF-8"))
if (mb_detect_encoding($coValue) != "UTF-8" || !mb_check_encoding($coValue, "UTF-8")) {
$coValue = utf8_encode($coValue);
}
$coins .= "&" . $coKey . "=" . urlencode($coValue);
}
$coins .= "%26ctx_enc%3Dinfo%3Aofi%2Fenc%3AUTF-8";
$coinsSpan = "<span class=\"Z3988\" title=\"" . $coins . "\"></span>";
return $coinsSpan;
}
示例14: scrapeMarketGroup
function scrapeMarketGroup($url)
{
global $visitedIds;
$html = scraperWiki::scrape($url);
$html = str_replace("\n", "", $html);
preg_match_all("|<a href=\"/importing/61000746/marketgroup/(\\d+?)/\">(.+?)</a>|s", $html, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$groupId = $match[1];
$groupName = html_entity_decode($match[2]);
//echo $groupName."\n";
if (!in_array($groupId, $visitedIds)) {
$visitedIds[] = $groupId;
scrapeMarketGroup("http://goonmetrics.com/importing/61000746/marketgroup/" . $groupId . "/");
}
}
preg_match_all("|<tr(.*?)>(.*?)<td(.*?)><a href=\"http://games.chruker.dk/eve_online/item.php\\?type_id=(.+?)\" target=\"_blank\">(.*?)<span class=\"dot\" onclick=\"CCPEVE.showMarketDetails\\((.*?)\\)\">(.+?)</span>(.*?)</td>(.*?)<td(.*?)>(.+?)</td>(.*?)<td(.*?)>(.*?)</td>(.*?)<td(.*?)>(.+?)</td>(.*?)<td(.*?)>(.*?)</td>(.*?)<td(.*?)>(.*?)</td>(.*?)<td(.*?)>(.*?)</td>(.*?)<td(.*?)>(.*?)</td>(.*?)<td(.*?)>(.*?)</td>(.*?)</tr>|s", $html, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$item = array("itemId" => trim($match[4]), "name" => trim(mb_check_encoding($match[7], 'UTF-8') ? $match[7] : utf8_encode($match[7])), "weekVol" => trim(mb_check_encoding($match[11], 'UTF-8') ? $match[11] : utf8_encode($match[11])), "k6Stock" => trim(mb_check_encoding($match[17], 'UTF-8') ? $match[17] : utf8_encode($match[17])));
$item['weekVol'] = str_replace(",", "", $item['weekVol']);
$item['k6Stock'] = str_replace(",", "", $item['k6Stock']);
$saved = false;
$delay = 0;
while (!$saved && $delay < 600) {
try {
@scraperwiki::save_sqlite(array('itemId'), $item, 'eve_goonmetrics');
$saved = true;
} catch (Exception $e) {
sleep(10);
$delay++;
}
}
}
}
示例15: apply
public function apply($value)
{
if (mb_check_encoding($value, 'UTF-16') && mb_substr_count($value, "") > 0) {
$value = mb_convert_encoding($value, 'UTF-8', 'UTF-16');
}
return $value;
}