php用正则去掉空格的方法:首先创建一个PHP示例文件;然后使用正则表达式“preg_replace('/[\n\r\t]/','',$html);”去掉空格即可。

推荐:《PHP视频教程》
php用正则去掉空格
php的str_replace函数把替换掉
方法如下
$html="<p>fdasf</p>";
echo $string = str_replace(array("<p>","","</p>"),"",$html);
br<http:></http:>
若是<p> 内容</p>替换成<p>内容</p><p>
content</p>替换成<p>contend</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK"><img
src="https://img.php.cn/upload/skill/001/246/273/6adaf20e352aa140.jpg" alt="Build MCP servers and clients for PHP applications with the official PHP SDK" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK" class="overflowclass">Build MCP servers and clients for PHP applications with the official PHP SDK</a>
<p class="overflowclass">使用官方Model Context Protocol PHP SDK将PHP应用程序功能暴露为MCP工具、资源和提示,用于代理工作流。</p>
</div>
<a rel="nofollow" href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div>(空格是tab键和空格键 混合的 都有可能)方法如下
$html=preg_replace('/[\n\r\t]/','',$html);//去空格
若是<p>后面跟了若干个,再是内容
</p><p> 内容</p>
替换成<p>内容</p>
<p> content</p>
替换成<p>contend</p>
<?php
$html="<p>
内容替换成<p>内容</p>
<p>content</p>替换成<p>contend</p>";方法如下
$html=trim($html);
$html=str_replace(PHP_EOL,"",$html);
$html=str_replace(" ","",$html);
$html=preg_replace('/\s+/','',$html);
$html=preg_replace('/[\n\r\t]/','',$html);
echo "{$html}";
?>
str_replace("<p><br>","",$htmlstr);</p>
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!










