preg_replace 总是 return 原始字符串
preg_replace always return original string
我想用'<thead>'
形式替换'<tbody>'
:
<table class="table table-bordered table-responsive" style="width: 100%;">
<tbody>
这是我的代码:
$reg_exp = "/(^<table.+.(?:[n])?(?:.+)?)(<tbody>)/";
$replace_with = "/1<thead>";
echo $input = '<table class="table table-bordered table-responsive" style="width: 100%;">
<tbody>';
$final = preg_replace($reg_exp, $replace_with, $input);
var_dump($final);
它打印:
string(83) "<table class="table table-bordered table-responsive" style="width: 100%;">
<tbody></tbody></table>
我不知道怎么了!拜托,有人可以指导我如何做到这一点吗?谢谢
你不应该为此使用正则表达式...但你的问题是 .
不匹配没有 s
修饰符的新行。
$reg_exp = "/(^<table.+.(?:[n])?(?:.+)?)(<tbody>)/s";
https://regex101.com/r/pV7XRd/1/ vs. https://regex101.com/r/pV7XRd/2/
您还应该使用 </code> 或 <code>\1
进行替换。
$reg_exp = "/(^<table.+.(?:[n])?(?:.+)?)(<tbody>)/s";
$replace_with = '<thead>';
$input = '<table class="table table-bordered table-responsive" style="width: 100%;">
<tbody>';
$final = preg_replace($reg_exp, $replace_with, $input);
var_dump($final);
我想用'<thead>'
形式替换'<tbody>'
:
<table class="table table-bordered table-responsive" style="width: 100%;">
<tbody>
这是我的代码:
$reg_exp = "/(^<table.+.(?:[n])?(?:.+)?)(<tbody>)/";
$replace_with = "/1<thead>";
echo $input = '<table class="table table-bordered table-responsive" style="width: 100%;">
<tbody>';
$final = preg_replace($reg_exp, $replace_with, $input);
var_dump($final);
它打印:
string(83) "<table class="table table-bordered table-responsive" style="width: 100%;">
<tbody></tbody></table>
我不知道怎么了!拜托,有人可以指导我如何做到这一点吗?谢谢
你不应该为此使用正则表达式...但你的问题是 .
不匹配没有 s
修饰符的新行。
$reg_exp = "/(^<table.+.(?:[n])?(?:.+)?)(<tbody>)/s";
https://regex101.com/r/pV7XRd/1/ vs. https://regex101.com/r/pV7XRd/2/
您还应该使用 </code> 或 <code>\1
进行替换。
$reg_exp = "/(^<table.+.(?:[n])?(?:.+)?)(<tbody>)/s";
$replace_with = '<thead>';
$input = '<table class="table table-bordered table-responsive" style="width: 100%;">
<tbody>';
$final = preg_replace($reg_exp, $replace_with, $input);
var_dump($final);