Reputation: 13
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?
Upvotes: 1
Views: 155
Reputation: 76656
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 />"
Upvotes: 1
Reputation: 32127
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);
Upvotes: 1