Ricardo Capistran Ricardo Capistran - 14 days ago 6
PHP Question

How to remove info from inside tags from a variable

I have this text in a variable, lets say $textvariable:

<p>Hello everybody I would like</p>
<p><output tipo="youtube" valor="5ZzklOEGW0w"><em>youtube: 5ZzklOEGW0w</em></output></p>
<p>to party a lot</p>
<p><output tipo="youtube" valor="RW2QkfIytRA"><em>youtube: RW2QkfIytRA</em></output></p>
<p>Tonight</p>


And I would like to remove what's insede and leave it like this:

<p>Hello everybody I would like</p>
<p><output tipo="youtube" valor="5ZzklOEGW0w"></output></p>
<p>to party a lot</p>
<p><output tipo="youtube" valor="RW2QkfIytRA"></output></p>
<p>Tonight</p>


I know the regex to find the info inside:

#<output>(.*?)</output>#


But how can I remove all information inside this tags? That can be more than one or two of this in a variable

Thanks!

Answer

You can use preg_replace

preg_replace('/(<output[^>]*>)(.*?)(<\/output>)/i', '$1$3', $html);
Comments