What is the usage of php regex?

藏色散人
Release: 2023-03-13 14:02:02
Original
2336 people have browsed it

php正则的用法:1、创建一个PHP示例文件;2、定义一个数组;3、通过正则表达式“preg_grep("/^(\d+)?\.\d+$/",$array);”进行替换即可。

What is the usage of php regex?

本文操作环境:Windows7系统、PHP7.1版、DELL G3电脑

php正则的使用

函数 描述
preg_filter 执行一个正则表达式搜索和替换
preg_grep 返回匹配模式的数组条目
preg_last_error 返回最后一个PCRE正则执行产生的错误代码
preg_match_all 执行一个全局正则表达式匹配
preg_match 执行一个正则表达式匹配
preg_quote 转义正则表达式字符
preg_replace_callback_array 执行一个正则表达式搜索并且使用一个回调进行替换
preg_replace_callback 执行一个正则表达式搜索并且使用一个回调进行替换
preg_replace 执行一个正则表达式的搜索和替换
preg_split 通过一个正则表达式分隔字符串

preg_filter

preg_grep

<?php
$array=array(1,2,3.4,53,7.9,.4);
$fl_array=preg_grep("/^(\d+)?\.\d+$/",$array);
print_r($fl_array);
Copy after login

preg_mactch_all

<?php
$userinfo = "Name: <b>PHP</b> <br> Title: <b>Programming Language</b>";
preg_match_all ("/<b>(.*)<\/b>/U", $userinfo, $pat_array);
print_r($pat_array[0]);
?>
Copy after login
<?php
//\\2是一个后向引用的示例. 这会告诉pcre它必须匹配正则表达式中第二个圆括号(这里是([\w]+))
//匹配到的结果. 这里使用两个反斜线是因为这里使用了双引号.
$html = "<b>bold text</b><a href=howdy.html>click me</a>";
 
preg_match_all("/(<([\w]+)[^>]*>)(.*?)(<\/\\2>)/", $html, $matches, PREG_SET_ORDER);
 
foreach ($matches as $val) {
    echo "matched: " . $val[0] . "\n";
    echo "part 1: " . $val[1] . "\n";
    echo "part 2: " . $val[2] . "\n";
    echo "part 3: " . $val[3] . "\n";
    echo "part 4: " . $val[4] . "\n\n";
}
?>
Copy after login

 推荐学习:《PHP视频教程

The above is the detailed content of What is the usage of php regex?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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