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

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!