문제

I need to remove all whitespaces between BR tags

This is my String:

<br /> <br />  <br />    <br />

Output Should be:

<br /><br /><br /><br />

Here is my Code:

$str = preg_replace("%<br />\s*<br />%", "<br />", $str);

This should work to my knowledge but I can't get it to work.

Any suggest should be appreciated.

Is for remove the spaces in wp tittle http://mp3goo.com, or is there any other way to clean the Title in wordpress?

도움이 되었습니까?

해결책

Here's a more robust pattern.

\s*(<br ?\/?>)+\s*

It matches all the following:

  • <br>
  • <br >
  • <br/>
  • <br />

Usage:

$str = preg_replace('#\s*(<br ?\/?>)+\s*#', '<br />', $str);
header('Content-Type: text/plain');
var_dump($str);

Output:

string(24) "<br /><br /><br /><br />"

Demo

다른 팁

Since the tags are self-closing, you'll want to replace the white-space at either side.

$str = preg_replace("%\s*<br />\s*%", "<br />", $str);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top