如何修剪所有领先/尾随< br>使用php的代码 [英] How to trim all leading/trailing <br> code using php

查看:84
本文介绍了如何修剪所有领先/尾随< br>使用php的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用PHP删除字符串中的所有前导和尾随< br>



以下是一个例子

 < br>< br> ; 
你好< br>
世界< br>
< p>这是一条讯息< br> ...< / p>
< br>< br>< br>< br>

我想返回

 你好<峰; br> 
世界< br>
< p>这是一条讯息< br> ...< / p>

我尝试做以下操作:

  echo trim($ str,'< br>'); 

但它不会删除它们。如何删除新的html代码?

解决方案使用 preg_replace 开始 ^ 和结束 $ 锚点:

  $ string = preg_replace('/ ^(< br>){0,} |(< br>){0,} $ /','',$ string); 

或者对于多行:

  $ string = preg_replace('/ ^(< br>){0,} |(< br>){0,} $ / m','',$ string); 

您也可以 trim() it multiple次:

  while($ string!==($ string = trim($ string,'< br>')) ){} 


I am trying to remove all leading and trailing <br> in a string using PHP.

Here is an example

<br><br>
Hello<br>
World<br>
<p>This is a message<br>...</p>
<br><br><br><br>

I want to return

Hello<br>
World<br>
<p>This is a message<br>...</p>

I tried to do the following

echo trim($str, '<br>');

But it does not remove them. How can I remove the new line html code?

解决方案

Use preg_replace with the beginning ^ and end $ anchors:

$string = preg_replace('/^(<br>){0,}|(<br>){0,}$/', '', $string);

Or for multiple lines:

$string = preg_replace('/^(<br>){0,}|(<br>){0,}$/m', '', $string);

You could also trim() it multiple times:

while($string !== ($string = trim($string, '<br>'))){}

这篇关于如何修剪所有领先/尾随&lt; br&gt;使用php的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆