preg_replace返回空字符串

时间:2018-07-18 03:53:48

标签: php regex wordpress

原始内容:

<thead>
<tr style="height: 24px;">
<td style="width: 50%; height: 24px;">hello</td>
<td style="width: 50%; height: 24px;">world</td>
</tr>

过滤后的内容:

<thead>
<tr style="height: 24px;">
<td style="width: 50%; height: 24px;">hello</td>
<td style="width: 50%; height: 24px;">world</td>
</tr>
</thead>
<tbody>   

我的代码:

    $reg_exp = "/(<thead>(?:(?:[\n])?.*)*)(<\/tr>)/s"; 
    $replace_with = '$1</tr></thead><tbody>';
    $content = preg_replace($reg_exp, $replace_with, $content);
    return $content;

但是这返回空字符串。我不知道出什么事了。有想法吗?

更新

N.B:我正在wordpress过滤器挂钩中使用此preg_match:

add_filter( 'the_content', function( $content ) {
  $content = str_replace('<ul>','<ul class="list-group">',$content);
  $content = str_replace('<ul >','<ul class="list-group">',$content);
  $content = str_replace('<li>','<li class="list-group-item">',$content);
  $content = str_replace('<li >','<li class="list-group-item">',$content);
  $content = str_replace('</ul>','</ul><div class="gap"> </div>',$content);
  $content = str_replace('<p><img','<img',$content);
  $content = str_replace('<img','<img class="center-img"',$content);
    $reg_exp = "/(^<table.+.(?:[n])?(?:.+)?)(<tbody>)/s";
    $replace_with = '$1<thead>';
    $content = preg_replace($reg_exp, $replace_with, $content);

    $reg_exp = "/(<thead>(?:(?:[\n])?.*)*)(<\/tr>)/s";
    $replace_with = '$1</tr></thead><tbody>';
    $content = preg_replace($reg_exp, $replace_with, $content);
    return $content;
});

0 个答案:

没有答案