search
Homephp教程php手册PHP判断字符串编码并且获取字符串中的中文 + 清除空格

参考URL:http://www.w3.org/International/questions/qa-forms-utf-8.en.php $result = preg_match(’%^(?: [\x09\x0A\x0D\x20-\x7E] # ASCII | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs | [\xE1

参考URL:http://www.w3.org/International/questions/qa-forms-utf-8.en.php

$result = preg_match(’%^(?:
[\x09\x0A\x0D\x20-\x7E] # ASCII
| [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
| \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
| \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
| \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
| [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
| \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
)*$%xs’, $string);

如果$result为真,则是UTF-8编码的字符串,否为ANSI

以上面为条件,匹配出字符串中的中文

if ($result) {
preg_match_all(“/[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}/”, $str, $arr);
print_r($arr[0]);
} else {
preg_match_all(“/[\x80-\xFF]./”, $str, $arr);
print_r($arr[0]);
}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

PHP清除空格

日常我们处理数据时经常会产生额外的空格,如果你想进行诸如比较字符串时,就会引致问题;同时也浪费额外的储存空间。

如何除掉空格?也许你首先会想到PHP内建函数trim()。没错,它处理字符的始末部分确实有效,但是,这种情况下它就办不到了:将多个空格变为一个空格,将空格变为有序的规则的队列等等…

于是,正则表达式就派上用场了。看看下面的代码:

$str = ” This line contains\tliberal \r\n use of   whitespace.\n\n”;

// 首先去掉头尾空格
$str = trim($str);

// 接着去掉两个空格以上的
$str = preg_replace(’/\s(?=\s)/’, ‘’, $str);

// 最后将非空格替换为一个空格
$str = preg_replace(’/[\n\r\t]/’, ‘ ‘, $str);

使用上面的例子可以去掉所有多余的空格。首先使用TRim()去头尾空格,接着用preg_replace()去掉重复的空格。
当中的(?=)表示只匹配后面的空格跟随前面的空格的空格。


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools