PHP заменил тег с нестандартными текстами не работает - PullRequest
0 голосов
/ 06 января 2019

Я работаю над сценарием PHP preg_replace, чтобы заменить тег новыми текстами

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 голосов
/ 06 января 2019

Ваше выражение должно быть как

<?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

...