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

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

May 25, 2016 pm 04:55 PM

下面我们给出一个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 (日文)



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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)