正则匹配 模式修正符 小写u的问题 php

WBOY
Release: 2016-06-23 14:39:22
Original
1022 people have browsed it

最近看到一段代码:

<?php$str = '你好,世界dd';preg_match_all('/./us', $str, $match);echo count($match[0])."<br />";  ?>
Copy after login

上网查了不少资料,但对于php正则表达式中的模式修正符u实在有些不明白,求解啊……


回复讨论(解决方案)

u :unicode 的缩写,表示待匹配的串是一个符合 unicode 编码规则的串,比如 utf-8 编码的串
在 u 修饰符下,一个汉字被当做一个字符被处理。\w 有原来的 [_0-9A-Za-z] 扩展到汉字

\w 由原来的 [_0-9A-Za-z] 扩展到汉字,
\w是匹配任意一个数字、字母、下划线的,扩展到汉字是什么意思?

$s = '汉字abc';preg_match_all('/\w/', $s, $r); //没有 u 修饰print_r($r);
Copy after login
Copy after login

Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
)

)
$s = '汉字abc';preg_match_all('/\w/u', $s, $r); //有 u 修饰print_r($r);
Copy after login
Copy after login

Array
(
[0] => Array
(
[0] => 汉
[1] => 字
[2] => a
[3] => b
[4] => c
)

)

$s = '汉字abc';preg_match_all('/\w/', $s, $r); //没有 u 修饰print_r($r);
Copy after login
Copy after login

Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
)

)
$s = '汉字abc';preg_match_all('/\w/u', $s, $r); //有 u 修饰print_r($r);
Copy after login
Copy after login

Array
(
    [0] => Array
        (
            [0] => 汉
            [1] => 字
            [2] => a
            [3] => b
            [4] => c
        )

)
版主大大 写个适合新手学习的正则吧。
还有.net的正则和php的正则区别大吗?
http://deerchao.net/tutorials/regex/regex.htm
这个人写的.net通俗易懂 ,求版主大大同样来个。。。

感谢版主,明白了……

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template