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

php – preg_match用于评论的某些部分

发布时间:2020-12-13 22:46:08 所属栏目:PHP教程 来源:网络整理
导读:我想找到评论的某些部分.以下是一些例子 /* * @todo name another-name taskID *//* @todo name another-name taskID */ 目前我使用以下正则表达式 '/*[[:space:]]*(?=@todo(.*))/i' 这返回以下内容: * @todo name another-name taskIDor* @todo name anot
我想找到评论的某些部分.以下是一些例子

/*
 * @todo name another-name taskID
 */

/* @todo name another-name taskID */

目前我使用以下正则表达式

'/*[[:space:]]*(?=@todo(.*))/i'

这返回以下内容:

* @todo name another-name taskID
or
* @todo name another-name taskID */

我怎么能得到@todo和名字,但不是任何星号?

期望的输出

@todo name another-name taskID
@todo name another-name taskID

解决方法

试试这个:

$str=<<<STR
/*
 * @todo name another-name taskID 1
 */

/* @todo name another-name taskID 2 */
STR;
$match=null;
preg_match_all("/*[[:space:]]*(@todo[^(*/$)]*)/i",$str,$match,PREG_SET_ORDER);
print_r($match);

回声

Array
(
    [0] => Array
        (
            [0] => * @todo name another-name taskID 1

            [1] => @todo name another-name taskID 1

        )

    [1] => Array
        (
            [0] => * @todo name another-name taskID 2 
            [1] => @todo name another-name taskID 2 
        )

)

虽然不是一个完美的解决方案(注意$match [0] [0]中的行结尾).

(编辑:李大同)

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

    推荐文章
      热点阅读