doupang5433 2018-06-28 16:20
浏览 15
已采纳

从括号中找到正则表达式php

I need help in regex php

It is necessary to use the number from parentheses, the parentheses are repeated in some cases

Example of two different strings:

  1. 2.0 16V Quadrifoglio (114 kW / 155 PS)
  2. 1.4 TB (940FXB1A) (125 kW / 170 PS)

I needed it to look like this:

  1. 2.0 16V Quadrifoglio 155 WORD
  2. 1.4 TB 170 WORD

I have code

  1. $text = '2.0 16V Quadrifoglio (114 kW / 155 PS)';
  2. preg_match('#\((.*?)\)#', $text, $match);
  3. print $match[1];

And results is:

114 kW / 155 PS

Please help to find number from parentheses

  • 写回答

2条回答 默认 最新

  • dongzhang4301 2018-06-28 16:36
    关注

    You need to capture just the number after the /, and replace the whole parenthesis expression with that.

    $newText = preg_replace('#\([^)]*/\s*([^)]*)\)#', '$1', $text);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

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

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

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

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

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

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

客服 返回
顶部