Marcello Impastato
Marcello Impastato

Reputation: 2281

How remove multiple substring from two tags

Gived a string so formed:

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

I want remove all between <tag> and </tag> and to have in output:

hello Rose and John. 

how i can it?

Upvotes: 4

Views: 50

Answers (2)

Pedro Lobito
Pedro Lobito

Reputation: 98921

I guess you can use:

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

Upvotes: 6

Abela
Abela

Reputation: 1233

If you do not want to use DOM, you could 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.

Upvotes: 0

Related Questions