Remove WhiteSpaces Between BR tags

I need to remove all whitespaces between BR tags

This is my String:

Read More
<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?

Related posts

Leave a Reply

2 comments

  1. 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

  2. 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);