doutiao2540 2018-05-01 04:18
浏览 11
已采纳

如何替换后跟数字的#标签?

I want to replace as FALSE where the string contains a # followed by an integer.

This is my code:

$newlogicexpression = '#1 and (1743327.12 >  10)';
if( strpos( $newlogicexpression, '#' ) !== false ) {
    $newlogicexpression = str_replace('#', 'FALSE', $newlogicexpression);
    $this->logger->debug($newlogicexpression);
}

My expected result is: FALSE and (1743327.12 > 10)

My current output is: FALSE1 and (1743327.12 > 10)

Based on the post method, the integer that follows the # may differ.

The replacement need to happen at any position in the string.

  • 写回答

2条回答 默认 最新

  • dsgtew3241 2018-05-01 04:31
    关注

    There are many ways to do that.
    For example, you can use this regular expression: #\d+

    Therefore:

    $newlogicexpression = '#1 and (1743327.12 >  10) and #2';
    if( strpos( $newlogicexpression, '#' ) !== false ) {
    
        $newlogicexpression = preg_replace('/#\d+/', 'FALSE', $newlogicexpression);
        $this->logger->debug($newlogicexpression);
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部