doutiao2540 2018-05-01 12: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 12: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条)

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?