1. preg_match 执行匹配正则表达式
实例
$pattem = '/[0-9]/'; $subject = '11112222333344445555666677778888999900000 aaabbbcccdddeeefffggghhhiiijjjjkkkk'; $a = preg_match($pattem,$subject,$matches); var_dump($a); // 输出匹配次数 var_dump($matches); // 输出匹配结果
运行实例 »
点击 "运行实例" 按钮查看在线实例
运行效果图
总结:preg_match 只匹配一次
2. preg_match_all 执行一个全局正则表达式匹配
实例
$pattem = '/[0-9]/'; $subject = '11112222333344445555666677778888999900000 aaabbbcccdddeeefffggghhhiiijjjjkkkk'; $a = preg_match_all($pattem,$subject,$matches); var_dump($a); // 输出匹配次数 var_dump($matches); // 输出匹配结果
运行实例 »
点击 "运行实例" 按钮查看在线实例
运行效果图
总结:匹配多次,只要有数字的都匹配
3. preg_replace 执行一个正则表达式的搜索和替换
示例1:单个替换
实例
$pattem = '/ouyangke/'; $replacement = '欧阳克'; $subject = '11112222333344445555666677778888999900000 aaabbbcccdddeeefffggghhhiiijjjjkkkk llllmmmmssssooopppqqqqrrrrssswwwvvvzzzxxxx ouyangke ouyangke ouyangke zhulaoshi zhulaoshi zhulaoshi ximen ximen ximen '; $a = preg_replace($pattem, $replacement, $subject); var_dump($a);
运行实例 »
点击 "运行实例" 按钮查看在线实例
运行效果图
示例2:数组批量替换
实例
<?php $pattem = array('/ouyangke/','/ximen/'); $replacement = array('欧阳克','西门大官人'); $subject = array( '11112222333344445555666677778888999900000 aaabbbcccdddeeefffggghhhiiijjjjkkkk llllmmmmssssooopppqqqqrrrrssswwwvvvzzzxxxx ouyangke ouyangke ouyangke ', 'ouyangke ouyangke ouyangke zhulaoshizhulaoshi ximen ximen ximen ' ); $a = preg_replace($pattem, $replacement, $subject); var_dump($a);
运行实例 »
点击 "运行实例" 按钮查看在线实例
运行效果图
4. preg_filter 执行一个正则表达式搜索和替换
示例1:
实例
<?php $pattem = '/ouyangke/'; $replacement = '欧阳克'; $subject = '11112222333344445555666677778888999900000 aaabbbcccdddeeefffggghhhiiijjjjkkkk llllmmmmssssooopppqqqqrrrrssswwwvvvzzzxxxx ouyangke ouyangke ouyangke zhulaoshi zhulaoshi zhulaoshi ximen ximen ximen '; $a = preg_filter($pattem, $replacement, $subject); var_dump($a);
运行实例 »
点击 "运行实例" 按钮查看在线实例
运行效果图
示例2:
实例
<?php $pattem = array('/ouyangke/','/ximen/'); $replacement = array('欧阳克','西门大官人'); $subject = array( '11112222333344445555666677778888999900000 aaabbbcccdddeeefffggghhhiiijjjjkkkk llllmmmmssssooopppqqqqrrrrssswwwvvvzzzxxxx ouyangke ouyangke ouyangke ', 'ouyangke ouyangke ouyangke zhulaoshizhulaoshi zhulaoshi ximen ximen ximen ' ); $a = preg_filter($pattem, $replacement, $subject); var_dump($a);
运行实例 »
点击 "运行实例" 按钮查看在线实例
5. preg_quote 转义正则表达式字符
实例
<?php $subject = '^ouyangke$zhulaoshi=miejue!\ximen&[] '; $a = preg_quote($subject); var_dump($a);
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php $subject = '^ouyangke$zhulaoshi=miejue!\ximen&[] '; $a = preg_quote($subject); var_dump($a);
运行实例 »
点击 "运行实例" 按钮查看在线实例
6. preg_grep 返回匹配模式的数组条目
实例
<?php $pattem = '/^(\d+)?\.\d+$/'; $array = array(1, 2, 3.4, 53, 7.9); $a = preg_grep($pattem,$array); var_dump($a);
运行实例 »
点击 "运行实例" 按钮查看在线实例
7. preg_split 通过一个正则表达式分隔字符串
实例
<?php $pattem = '/[\s,]+/'; $subject = 'ouyangke ouyangke zhulaoshi zhulaoshi ximen ximen'; $a = preg_split($pattem,$subject); var_dump($a);
运行实例 »
点击 "运行实例" 按钮查看在线实例