I have some HTML content and I would like to replace a tag:
<span class='c1'>MY TEXT</span>
And keep MY TEXT.
I tried with:
$result = preg_replace('/(<span class=\'c1\'>)(.*)(<\/span>)/', '$2', $my_string);
But the closed tag still remains?
Can you help me and EXPLAIN where is my mistake? I would like to improve myself!
Thank you
Try using a lazy match
(.*?)instead of a greedy match(.*).Greedy match means it will match as much as possible before finishing, so if you have another
</span>somewhere, it will match that instead. For example:Using a greedy match:
Using a lazy match: