正则表达式是处理字符串的强大工具,PHP中也提供了很多常用的正则函数。本文将介绍PHP正则的相关函数,包括基本匹配函数、替换函数、模式修饰符和元字符等。
基本匹配函数
1. preg_match(): preg_match()函数是PHP中最常用的正则表达式函数之一,它用来进行模式匹配,并返回匹配结果。
语法:preg_match($pattern,$subject,$matches);
其中,$pattern表示正则表达式模式,$subject表示要匹配的字符串,$matches则是用来存储匹配结果的数组。如果匹配成功返回1,否则返回0。
举个例子:
$pattern = '/php/i';
$subject = 'Php is the best scripting language';
if (preg_match($pattern, $subject)) {
echo 'Match found';
} else {
echo 'Match not found';
}
2. preg_match_all(): preg_match_all()函数与preg_match()函数类似,区别在于它会匹配所有符合条件的结果,并返回匹配的次数。
语法:preg_match_all($pattern,$subject,$matches);
举个例子:
$pattern = '/php/i';
$subject = 'Php is the best scripting language. PHP is widely used for web development';
$count = preg_match_all($pattern, $subject, $matches);
echo $count;
输出结果:2
3. preg_replace(): preg_replace()函数用来替换匹配的结果。
语法:preg_replace($pattern,$replacement,$subject);
其中,$pattern表示要搜索的正则表达式模式,$replacement表示替换的字符串,$subject则是要被替换的字符串。
举个例子:
$pattern = '/blue/i';
$replacement = 'red';
$subject = 'Blue is my favorite color';
echo preg_replace($pattern, $replacement, $subject);
输出结果:Red is my favorite color
4. preg_split(): preg_split()函数用来将字符串分割成数组,使用正则表达式进行匹配。
语法:preg_split($pattern,$subject);
其中,$pattern表示正则表达式模式,$subject表示要被分割的字符串。
举个例子:
$pattern = '/\s+/';
$subject = 'apple banana cherry';
$array = preg_split($pattern, $subject);
print_r($array);
输出结果:Array ( [0] => apple [1] => banana [2] => cherry )
模式修饰符
在使用正则表达式时,有时需要加上一些模式修饰符来改变正则表达式的行为。
1. i: 忽略大小写
$pattern = '/php/i';
$subject = 'Php is the best scripting language';
if (preg_match($pattern, $subject)) {
echo 'Match found';
} else {
echo 'Match not found';
}
2. g: 全局匹配
$pattern = '/php/';
$subject = 'Php is the best scripting language. PHP is widely used for web development';
$count = preg_match_all($pattern, $subject, $matches);
echo $count;
3. m: 多行匹配
$pattern = '/^php/im';
$subject = "PHP is the best scripting language\nIt is widely used for web development";
$count = preg_match_all($pattern, $subject, $matches);
echo $count;
4. s: 让 . 匹配所有字符,包括换行符
$pattern = '/php . language/s';
$subject = "PHP is the best scripting \nlanguage,\nIt is widely used for web development";
if (preg_match($pattern, $subject)) {
echo 'Match found';
} else {
echo 'Match not found';
}
元字符
在正则表达式中,有一些特殊的字符表示特定的意义,称为元字符。
1. .: 匹配任意字符,除了换行符
$pattern = '/.at/';
$subject = 'cat bat rat';
if (preg_match($pattern, $subject)) {
echo 'Match found';
} else {
echo 'Match not found';
}
2. ^: 匹配字符串的开始
$pattern = '/^php/';
$subject = 'PHP is the best scripting language';
if (preg_match($pattern, $subject)) {
echo 'Match found';
} else {
echo 'Match not found';
}
3. $: 匹配字符串的结束
$pattern = '/language$/';
$subject = 'PHP is the best scripting language';
if (preg_match($pattern, $subject)) {
echo 'Match found';
} else {
echo 'Match not found';
}
4. []: 匹配任意一个中括号内的字符
$pattern = '/[abc]/';
$subject = 'abc123';
if (preg_match($pattern, $subject)) {
echo 'Match found';
} else {
echo 'Match not found';
}
5. [^]: 匹配除了中括号内的字符以外的其他字符
$pattern = '/[^abc]/';
$subject = 'abc123';
if (preg_match($pattern, $subject)) {
echo 'Match found';
} else {
echo 'Match not found';
}
结语
本文介绍了PHP中几个常用的正则表达式函数,包括基本匹配函数、替换函数、模式修饰符和元字符等,希望能够帮助大家更好地掌握和应用正则表达式。 如果你喜欢我们三七知识分享网站的文章, 欢迎您分享或收藏知识分享网站文章 欢迎您到我们的网站逛逛喔!https://www.ynyuzhu.com/
发表评论 取消回复