加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

从php正则表达式提取匹配

发布时间:2020-12-13 14:07:33 所属栏目:PHP教程 来源:网络整理
导读:在perl正则表达式中,我们可以提取匹配的变量,如下所示. # extract hours,minutes,seconds $time =~ /(dd):(dd):(dd)/; # match hh:mm:ss format $hours = $1; $minutes = $2; $seconds = $3; 在php中怎么做? $subject = "E:contact@customer.com I:10
在perl正则表达式中,我们可以提取匹配的变量,如下所示.
# extract hours,minutes,seconds
   $time =~ /(dd):(dd):(dd)/; # match hh:mm:ss format
   $hours = $1;
   $minutes = $2;
   $seconds = $3;

在php中怎么做?

$subject = "E:contact@customer.com I:100955";
$pattern = "/^E:/";
if (preg_match($pattern,$subject)) {
    echo "Yes,A Match";
}

如何从那里提取电子邮件? (我们可以爆炸它得到它…但是想要一种通过正则表达式直接获取它的方法)?

尝试使用preg_match的命名子模式语法:
<?php

$str = 'foobar: 2008';

// Works in PHP 5.2.2 and later.
preg_match('/(?<name>w+): (?<digit>d+)/',$str,$matches);

// Before PHP 5.2.2,use this:
// preg_match('/(?P<name>w+): (?P<digit>d+)/',$matches);

print_r($matches);

?>

输出:

Array (
     [0] => foobar: 2008
     [name] => foobar
     [1] => foobar
     [digit] => 2008
     [2] => 2008 )

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读