-1

How can I convert all multiple white space inside any given HTML element to a single space using regex and preg_replace in php?

Eg: <div class="myClass" jsaction="UjQMac:.CLIENT" data-id="3739" >Edit</div>

Cleaned: <div class="myClass" jsaction="UjQMac" data-id="3739">Edit</div> All multiple spaces removed and only single spaces retained. Also, the > is replaced with a >

I've been trying unsuccessfully with this regex \<(\s+)\>. Can you help?

Edit: The regex (?:(\s{2,})|(\s>)) from the answer below works fine, but does not match only between < & >

Norman
  • 6,159
  • 23
  • 88
  • 141
  • 2
    should not be done using regex. Use HTML DOM parser – anubhava Oct 05 '20 at 09:53
  • @anubhava althought I agree with you, when people try to parse HTML with RegEx, I think this case is safe for RegEx usage. Or perhaps there's something I'm missing – Cid Oct 05 '20 at 10:00
  • 1
    [Don't parse HTML with RegEx](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – Wesley Smith Oct 05 '20 at 10:08
  • 2
    @Cid consider this html `
    Edit
    ` and how that might complicate a regex solution.
    – Wesley Smith Oct 05 '20 at 10:15
  • 1
    Good point @WesleySmith I didn't realized OP wanted to match the tag – Cid Oct 05 '20 at 11:20

1 Answers1

0

This will do it: (?:(\s{2,})|(\s>))

It matches any whitespace character that appears 2x or more often and also > with a leading .

See: https://regex101.com/r/NN9YUU/2/

Christian Baumann
  • 3,188
  • 3
  • 20
  • 37
  • I'm trying to get it to match only what's in between `<` & `>`. This'll match an entire string. It works great though. If you see the regex i'm using in my question `\<(\s+)\>`. I did try putting your solution into my regex, but it won't work. – Norman Oct 05 '20 at 10:06
  • https://regex101.com/r/NN9YUU/4/ – Norman Oct 05 '20 at 10:09
  • Can you help with the `<` & `>` part? – Norman Oct 05 '20 at 10:32
  • @Norman not really, any regex approach to this is doomed to failure. RegEx is not suited for this type of application. Any one you use, we'll be able to make it fail. – Wesley Smith Oct 05 '20 at 10:40
  • I won't be working with data that'll submitted by the public. This is only to cleanup a huge garbled output from some blog backup. – Norman Oct 05 '20 at 10:43
  • This issue isnt specific to data submitted by the public. Please read the post I linked to in my comment on the question – Wesley Smith Oct 05 '20 at 10:52
  • https://regex101.com/r/NN9YUU/5/ – Norman Oct 05 '20 at 12:37