remove whitespace from html string

Discussion in 'PHP' started by catapop, Nov 25, 2008.

  1. #1
    Hi. I've searched google and did'nt found what I am looking for. I don't know why because I think this is an easy solution.

    I want to remove all white spaces between tags from an html string

    I found a regex solution but it gives me a compilation error. can someone help me?

    here is the code:

    $input=preg_replace( "/(?:(?<=\>)|(?<=\/\))(\s+)(?=\<\/?)/","", $input );
    Code (markup):

    Thanks
     
    catapop, Nov 25, 2008 IP
  2. smatts9

    smatts9 Active Member

    Messages:
    1,089
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    88
    #2
    smatts9, Nov 25, 2008 IP
  3. catapop

    catapop Peon

    Messages:
    79
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    trim — Strip whitespace (or other characters) from the beginning and end of a string

    i want to remove whitespaces and new lines between all the tags :)
     
    catapop, Nov 26, 2008 IP
  4. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #4
    Your pattern seems a little complicated.

    preg_replace('#>\s+<#', '><', $str);
    Code (markup):
     
    joebert, Nov 26, 2008 IP