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 Arduino,利用modbus的RS485协议,进行对外置的温湿度传感器进行数据读取
  • ¥15 vhdl+MODELSIM
  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么