欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

网上一段简略的preg_match替换 不明

程序员文章站 2022-06-01 18:54:04
...
网上一段简单的preg_match替换 不明

$string = "Is is the cost of of gasoline going up up";
$pattern = "/\b([a-z]+) \\1\b/i";
if(preg_match($pattern, $string,$arr)){
print_r($arr);
echo preg_replace($pattern, '$1', $string);
}

输出

Array
(
[0] => Is is
[1] => Is
)
Is the cost of gasoline going up


我理解的preg_match($pattern,$repalce,$subject)是用pattern从subject匹配到各个分组,然后用replace规定的显示方式,重新输出
$pattern = "/\b([a-z]+) \\1\b/i" 匹配到的只有Is is 和Is
重新输出怎么会输出一整句话 Is the cost of gasoline going up
还有什么时候用$1 什么时候用\\1 获取子模式匹配的内容 在双引号和单引号里又有什么要求?
php?正则

网友评论

文明上网理性发言,请遵守 新闻评论服务协议

我要评论
  • 网上一段简略的preg_match替换 不明
  • 专题推荐

    作者信息
    网上一段简略的preg_match替换 不明

    认证0级讲师

    推荐视频教程
  • 网上一段简略的preg_match替换 不明javascript初级视频教程
  • 网上一段简略的preg_match替换 不明jquery 基础视频教程
  • 视频教程分类