Home  >  Article  >  Backend Development  >  What is the usage of php regex?

What is the usage of php regex?

藏色散人
藏色散人Original
2021-10-27 09:15:122346browse

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);

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]);
?>
<?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";
}
?>

 推荐学习:《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!

Statement:
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