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

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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?