Home > php教程 > php手册 > PHP正则表达式匹配字符串中的指定标签

PHP正则表达式匹配字符串中的指定标签

WBOY
Release: 2016-05-25 16:55:42
Original
1288 people have browsed it
下面我们给出一个PHP正则表达式匹配字符串中的指定标签实现程序代码,有需要学习的朋友可参考本教程。

在 PHP 应用中,正则表达式主要用于:

•正则匹配:根据正则表达式匹配相应的内容
•正则替换:根据正则表达式匹配内容并替换
•正则分割:根据正则表达式分割字符串

常用

preg_match_all()
preg_match_all() 函数用于进行正则表达式全局匹配,成功返回整个模式匹配的次数(可能为零),如果出错返回 FALSE 。

语法:

int preg_match_all( string pattern, string subject, array matches [, int flags ] )


实例

$str = "之二宽阔的甘家口东#标签1#标签2 #标签3。#标签4,都发$1234 ¥xc,cvm , ¥12,dflksjf如何#标签5.x

 代码如下 复制代码
#tag6.cvxcv“";
preg_match_all('/#([a-zA-Z0-9x7f-xff]+)/', $str, $mat);
print_r($mat);
 
preg_match("/[x{00a5}x{ffe5}](d+)/u", $str, $mat);
print_r($mat);

正则匹配中文汉字根据页面编码不同而略有区别:

•GBK/GB2312编码:[x80-xff>]+ 或 [xa1-xff]+
•UTF-8编码:[x{4e00}-x{9fa5}]+/u
例子:

 代码如下 复制代码

$str = "学习php是一件快乐的事。";
preg_match_all("/[x80-xff]+/", $str, $match);
//UTF-8 使用:
//preg_match_all("/[x{4e00}-x{9fa5}]+/u", $str, $match);
print_r($match);
?>

补充说明:

双字节字符编码范围

1. GBK (GB2312/GB18030)
x00-xff GBK双字节编码范围
x20-x7f ASCII
xa1-xff 中文 gb2312
x80-xff 中文 gbk

2. UTF-8 (Unicode)

u4e00-u9fa5 (中文)
x3130-x318F (韩文
xAC00-xD7A3 (韩文)
u0800-u4e00 (日文)



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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template