dousheng3364 2016-03-17 17:00
浏览 36
已采纳

无法在Php或Notepad ++中按行完成正则表达式替换

190 PALMER RD, HARMONY
MLS® 201512263 $239,900
2 ACRES/HEATED DBL GARAGE/HEAT PUMP
http://goo.gl/tVWDan

I want the above first line '190 PALMER RD, HARMONY' replaced with : <p style="color: #EF2F48"> <b> 190 PALMER RD, HARMONY </b> </p> using regex in PHP.

I used the following code. But doesn't work. Please help.

(^[0-9].*) - <p style="color: #EF2F48"> <b> \1 </b> </p>

I want the above second line 'MLS® 201512263 $239,900' replaced with : <p> MLS® 201512263 &nbsp;&nbsp; $239,900 </p> using regex.

I used the following code. But works.

(MLS®[0-9]*) (\$[0-9,]+) - <p> \1 &nbsp;&nbsp; \2 </p>

And I want the third line select by regex. I am not sure. I want like this. <p style="color: #000"> 2 ACRES/HEATED DBL GARAGE/HEAT PUMP </p>

Fourth line I selected like this way and replaced.

(http:*.*) - <p><a class="ui-link" style="color: blue;"href="\1" target="_blank">\1</a></p>

Please help me.
Manoj

  • 写回答

1条回答 默认 最新

  • dongzaijiao4863 2016-03-17 17:25
    关注

    Try following script:

    $rx = '/^([^
    ]+)\s*([^
    ]+) (\$[^
    ]+)\s*([^
    ]+)\s*([^
    ]+)$/sim';
    $replacement = '<p style="color: #EF2F48"><b>$1</b></p><p>$2&nbsp;&nbsp$3</p> <p style="color: #000">$3</p> <p><a class="ui-link" style="color: blue;"href="$4" target="_blank">$5</a></p>';
    
    $subject = ".... your test data ....";
    
    $result = preg_replace($rx, $replacement, $subject);
    

    Result:

    <p style="color: #EF2F48"><b>190 PALMER RD, HARMONY</b></p>
    <p>MLS® 201512263&nbsp;&nbsp$239,900</p>
    <p style="color: #000">$239,900</p> 
    <p>
    <a class="ui-link" style="color: blue;"href="2 ACRES/HEATED DBL GARAGE/HEAT PUMP" target="_blank">
    http://goo.gl/tVWDan</a>
    </p>
    

    See Demo here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度