php preg_replace,用span标记中的data属性替换所有span标记

I would like to replace all span tags in PHP with its data-snippet-php content. Example: Given string:

$str = 'text <span data-snippet-php ="do magic">Hello World</span> text';

Wanted string:

$str = 'text do magic text';

Replace the span tag with the data-snippet-php value.

This works so far in the example: https://regex101.com/r/XhaEA9/1/ However, it only works correctly with one span tag. Everything between the first and second match will be swallowed. What is the correct pattern?

$re = '/<span.*?data-snippet-php="(.*?)".*?>.*?<\/span>/m';
$str = 'HTML Code <span class="block" title="Label: Titel" data-snippet-php="<?php _e(\'Titel\',\'grid_test\') ?>" editable="false">[ Titel ]</span>: <span class="block" title="Datenfeld: Titel" data-snippet-bind="#: post_title #" editable="false">{ post_title }</span> text text text <span class="block" title="Label: Desc" data-snippet-php="<?php _e(\'Desc\',\'grid_test\') ?>" editable="false">[ Desc ]</span>: text text text';
$subst = '$1';
$result = preg_replace($re, $subst, $str);
$result ==> 
'HTML Code <?php _e('Titel','grid_test') ?>: <?php _e('Desc','grid_test') ?>: text text text';

This result is wrong, I want this result:

$result ==> 
'HTML Code <?php _e('Titel','grid_test') ?>: <span class="block" title="Datenfeld: Titel" data-snippet-bind="#: post_title #" editable="false">{ post_title }</span> text text text <?php _e('Desc','grid_test') ?>: text text text';

In your regex .*? is not enough, it matches untill data-snippet-php="(.*?)" of the following <span, you have to stop the seach before the first encountered >.

Change them with negated character class:

$str = <<<EOD
HTML Code <span class="block" title="Label: Titel" data-snippet-php="<?php _e('Titel','grid_test') ?>" editable="false">[ Titel ]</span>: 
<span class="block" title="Datenfeld: Titel" data-snippet-bind="#: post_title #" editable="false">{ post_title }</span> 
text text text 
<span class="block" title="Label: Desc" data-snippet-php="<?php _e(\'Desc\',\'grid_test\') ?>" editable="false">[ Desc ]</span>: text text text
EOD;

$re = '~<span[^>]*?data-snippet-php="([^"]*)"[^>]*>[^<]*</span>~sm';
//           ^^^^^^                   ^^^^^  ^^^^^ ^^^^^        ^ I've added s flag to deal with multilines
$subst = '$1';
$result = preg_replace($re, $subst, $str);
echo $result,"
";

Output:

HTML Code <?php _e('Titel','grid_test') ?>: 
<span class="block" title="Datenfeld: Titel" data-snippet-bind="#: post_title #" editable="false">{ post_title }</span> 
text text text 
<?php _e(\'Desc\',\'grid_test\') ?>: text text text