Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top