This question is NOT a duplicate and should not be marked as such!
I have done my research and have not found a solution. Standard replacements are not a solution here (How do I replace certain parts of my string? is not helpful). It is very annoying that every question asked on Stack Overflow is marked as duplicate and closed directly after less than 5 minutes. Obviously without even having read/understood the question.
Back to the problem:
Here's an example:
<?php
$string = '<span class="X">001</span>';
$string .= '<span class="X">444</span>';
$string .= '<span class="X">242</span>';
$string .= '<span class="X">334</span>';
?>
Now I want to replace every occurrence from <span class="X">
to <span>
(including the ! unknown ! number contained) with another string, for example <div title="yay">Icecream</div>
.
So that the result could be:
<?php
echo $string;
// outputs:
// <div title="yay">Icecream</div>
// <div title="yay">Icecream</div>
// <div title="yay">Icecream</div>
// <div title="yay">Icecream</div>
?>
How can this be achieved?