How to remove multiple substrings from two tags

Returned the generated string:

hello Rose <tag> Micheal </tag> and <tag> July </tag> John. 

I want to delete everything between <tag>and </tag>and have the output:

hello Rose and John. 

How can i do this?

+4
source share
2 answers

I think you can use:

$new_html = preg_replace('%<tag>.*?</tag> ?%si', '', $old_html);
+6
source

If you do not want to use the DOM , you can always use preg_replace to do this.

$content = 'hello Rose <tag> Micheal </tag> and <tag> July </tag> John.';

$content = preg_replace('/<tag>[^<]+<\/tag>/i', '', $content);

print $content; // returns: hello Rose and John.
0
source

Source: https://habr.com/ru/post/1675245/


All Articles