用nl2br处理了从数据库里取的数据,不想使用<br>换行,想使用<p></p>分段。
网上找到的方法:
$content = '<p>'.str_replace("<br />","</p><div class="aritcle_card flexRow">
<div class="artcardd flexRow">
<a class="aritcle_card_img" href="/xiazai/code/10982" title="MVM mall 网上购物系统"><img
src="https://img.php.cn/upload/webcode/000/000/004/176455800251815.jpg" alt="MVM mall 网上购物系统" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a href="/xiazai/code/10982" title="MVM mall 网上购物系统">MVM mall 网上购物系统</a>
<p>采用 php+mysql 数据库方式运行的强大网上商店系统,执行效率高速度快,支持多语言,模板和代码分离,轻松创建属于自己的个性化用户界面 v3.5更新: 1).进一步静态化了活动商品. 2).提供了一些重要UFT-8转换文件 3).修复了除了网银在线支付其它支付显示错误的问题. 4).修改了LOGO广告管理,增加LOGO链接后主页LOGO路径错误的问题 5).修改了公告无法发布的问题,可能是打压</p>
</div>
<a href="/xiazai/code/10982" title="MVM mall 网上购物系统" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span> </a>
</div>
</div><p>",$content).'</p>';
但是我的数据里还有别的一些标签,转换的时候就会有问题。
比如我有这样一段数据:
blahblah<br /> <pre class="brush:php;toolbar:false;"><code><br /> blahblah<br /> blahblah<br /> </code>
就会变成
立即学习“PHP免费学习笔记(深入)”;
<p>blahblah</p><p> <pre class="brush:php;toolbar:false;"><code></p><p> blahblah</p><p> </code>
这样我的<pre class="brush:php;toolbar:false;"><code></code></pre>代码块就被隔断了。
有没有什么办法不替换<pre class="brush:php;toolbar:false;"><code></code></pre>里的换行?
回复内容:
用nl2br处理了从数据库里取的数据,不想使用<br>换行,想使用<p></p>分段。
网上找到的方法:
$content = '<p>'.str_replace("<br />","</p><p>",$content).'</p>';
但是我的数据里还有别的一些标签,转换的时候就会有问题。
比如我有这样一段数据:
blahblah<br /> <pre class="brush:php;toolbar:false;"><code><br /> blahblah<br /> blahblah<br /> </code>
就会变成
立即学习“PHP免费学习笔记(深入)”;
<p>blahblah</p><p> <pre class="brush:php;toolbar:false;"><code></p><p> blahblah</p><p> </code>
这样我的<pre class="brush:php;toolbar:false;"><code></code></pre>代码块就被隔断了。
有没有什么办法不替换<pre class="brush:php;toolbar:false;"><code></code></pre>里的换行?
echo preg_replace('#([a-zA-Z]*)<br\s*\/>#', "<p>$1</p>", "blahblah<br />
<pre class="brush:php;toolbar:false;"><code><br />
blahblah<br />
blahblah<br />
</code>");
p和br都是标签,没有什么转换的必要,你想要<p></p>的效果也就是为了好看点,所以直接用css就行了,别折腾PHP了。例如
p br {margin-bottom: 1em;}
p pre br {margin-bottom: 0;}










