在你的正则表达式里
.*?
是不够的,它匹配到
data-snippet-php="(.*?)"
以下内容之一
<span
,你必须在第一次遇到前停止搜索
>
$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,"\n";
输出:
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