PHP用自定义文本替换了标记,但无法正常工作

时间:2019-01-06 04:08:30

标签: php regex preg-replace

我正在使用PHP preg_replace脚本来用一些新的tex替换标签

ex-我要替换里面的文本

我的代码在这里

$string= '<p style="margin: 0 0 16px;">Your order is shipped and 
    it’s on the way to your address, It will receive to you within 
   <tag>current_date format=”jS F Y” oddt=”” shipdays=”16″</tag></p>';

    $search = "/[^<tag>](.*)[^<\/tag>]/";

    $replace = "22nd January 2019 and 27th January";


    echo $sv= preg_replace($search,$replace,$string);

但是输出显示如下

22nd January 2019 and 27th January 201922nd January 2019 and 27th January 201922nd January 2019 and 27th January 201922nd January 2019 and 27th January 201922nd January 2019 and 27th January 201922nd January 2019 and 27th January 201922nd January 2019 and 27th January 201922nd January 2019 and 27th January 201922nd January 2019 and 27th January 201922nd January 2019 and 27th January 2019

enter image description here

我的字符串只有一个,但是脚本似乎替换了全文,任何人都可以知道此错误的原因和解决方法

或另一种方法。

非常感谢

1 个答案:

答案 0 :(得分:1)

您的表情应该像

<?php


$string= '<p style="margin: 0 0 16px;">Your order is shipped and 
    it’s on the way to your address, It will receive to you within 
   <tag>current_date format=”jS F Y” oddt=”” shipdays=”16″</tag></p>';

    $search = "/<tag>(.*)<\/tag>/";

    $replace = "22nd January 2019 and 27th January";


    echo $sv= preg_replace($search,$replace,$string);
?>

表达式/<tag>(.*)<\/tag>/表示<tag></tag>之间的任何文本。

enter image description here