Php preg_replace返回空字符串
原始内容:Php preg_replace返回空字符串,php,regex,wordpress,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;">
<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;
$reg_exp=“/((?:(?:[\n])?*))()/s”;
$replace_为=“$1”;
$content=preg\u replace($reg\u exp,$replace\u with,$content);
返回$content;
但是这个返回是空字符串。我搞不清楚到底出了什么问题。有什么想法吗
更新:
注意:我正在wordpress过滤器挂钩中使用这个preg_匹配:
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;
});
add_过滤器('the_content',函数($content){
$content=str_replace(“”,“”,$content);
$content=str_replace(“”,“”,$content);
$content=str_replace(“- ”,“
- ”,$content);
$content=str_replace(“
- ”,“
- ”,$content);
$content=str_replace(“
”、“
”、$content);
$content=str\u replace(“我们在这里解析有效的html吗?这是完整的输入字符串吗?这可以提供一些html修复:请澄清您的实际/真实输入数据。我们可以看到更多的代码吗?这可能是一个打字错误:我的代码为什么不能工作?问题。添加一些缺少的代码行时请参阅证据:我用完整的代码片段更新我的问题。您应该我不会用
str\u replace()
攻击dom节点。我建议使用dom解析器。^