dsyua2828 2018-09-05 15:58
浏览 169
已采纳

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';
  • 写回答

1条回答 默认 最新

  • duanpang2751 2018-09-06 09:54
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退