php下正则替换多个<br />

本文介绍了一种使用PHP通过正则表达式去除HTML内容中多余的&lt;p&gt;和&lt;br&gt;标签的方法。该方法首先移除空的段落标签,并进一步清理连续出现的换行标签。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

php下正则替换多个<br />

 

怎么实现php去掉开头结尾多余的p、br标签

 

 set_time_limit(3);

    $str ='<p>这这是内容这是内容这是内容这是内容这是内容这是内容这是内容是内容</p><p></p><p>换行了换行了换行了换行了换行了换行了换行了换行了换行了</p><p></p><p>&nbsp;</p><p></p><p></p><p></p><p></p>';

    $tmp = $str;

     while($str =preg_replace('/<p>[\s(<br\s*?\/*?>)(&nbsp;)]*<\/p>$/i','',$str)){

         if($str== $tmp) break;

         $tmp =$str;

     }

 

 $tmp = $result;
while($result = preg_replace('/<br\s*\/?>$/i','',$result)){
     if($result == $tmp) break;
     $tmp= $result;
 }
 echo ($result."\n\r");

 

 

$content=preg_replace(array('/\s*(<br\s*\/?\s*>\s*){2,}/im','/(<p>(\s|\s*<br\s*\/?\s*>\s*)*<\/p>)+/im'),array('\\1\\1',''),$content);

 

以原生php模板而不是html模板改写下文 <!DOCTYPE html> <html> <head> <title>调试信息</title> <style> .debug-section { margin: 20px 0; padding: 15px; border: 1px solid #ddd; border-radius: 5px; background-color: #f9f9f9; } .debug-section h2 { margin-top: 0; color: #333; border-bottom: 1px solid #eee; padding-bottom: 10px; } .debug-item { margin-bottom: 15px; padding: 10px; background-color: #fff; border: 1px solid #eee; border-radius: 3px; } .debug-item pre { background-color: #f5f5f5; padding: 10px; border-radius: 3px; overflow-x: auto; } .empty-message { color: #999; font-style: italic; } </style> </head> <body> <h1>系统调试信息</h1> <!-- Session 数据 --> <div class="debug-section"> <h2>Session 数据</h2> {if empty($sessionData)} <p class="empty-message">Session is empty</p> {else} {foreach $sessionData as $name => $value} <div class="debug-item"> <strong>Session Name:</strong> {$name|htmlspecialchars}<br> <strong>Value:</strong> <pre>{$value|print_r|htmlspecialchars}</pre> </div> {/foreach} {/if} </div> <!-- 普通 Cookie 数据 --> <div class="debug-section"> <h2>普通 Cookie 数据</h2> {if empty($cookieData)} <p class="empty-message">No cookies found</p> {else} {foreach $cookieData as $name => $value} {if $name != 'waxx' || !is_array($value)} <div class="debug-item"> <strong>Cookie Name:</strong> {$name|htmlspecialchars}<br> <strong>Value:</strong> <pre>{$value|print_r|htmlspecialchars}</pre> </div> {/if} {/foreach} {/if} </div> <!-- 原站点数组形式 Cookie --> <div class="debug-section"> <h2>原站点数组形式 Cookie (waxx)</h2> {if empty($waxxCookie)} <p class="empty-message">No waxx cookies found</p> {else} {foreach $waxxCookie as $key => $value} <div class="debug-item"> <strong>Cookie Name:</strong> waxx[{$key|htmlspecialchars}]<br> <strong>Value:</strong> <pre>{$value|print_r|htmlspecialchars}</pre> </div> {/foreach} {/if} </div> </body> </html>
07-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值