5

If there is one thing I can't understand ( or learn ), then that's preg_replace syntax. I need help removing all possible symbols ( space, tab, new line, etc. ) between > and <.

Meaning, I have such XML:

<?xml version=\"1.0\" encoding=\"UTF-8\"?><bl>  <snd>BANK</snd>    <rcv>ME</rcv>  <intid>773264</intid> <date>17072012</date></bl>

I need it to look:

<?xml version=\"1.0\" encoding=\"UTF-8\"?><bl><snd>BANK</snd><rcv>ME</rcv><intid>773264</intid><date>17072012</date></bl>

So far I came up with this:

$this -> data = preg_replace('\>(.*?)<\', '><', $data);

But it doesn't even come close to what I need. A solution would be appreciated.

Peon
  • 7,902
  • 7
  • 59
  • 100
  • possible duplicate of [converting ereg to preg (missing regex delimiters)](http://stackoverflow.com/questions/6270004/converting-ereg-expressions-to-preg) – mario Jul 17 '12 at 15:55

2 Answers2

7

You're close, you just need delimiters and to restrict your search for space characters:

preg_replace('#>\s+<#', '><', $data);

Where # is the delimiter character, and \s is shorthand for any space characters.

You can see it working in this example.

nickb
  • 59,313
  • 13
  • 108
  • 143
  • Will it also remove possible tab symbols? I updated the question. There should be only spaces, but who knows. – Peon Jul 17 '12 at 15:31
1

For removing spaces:

preg_replace('/\s\s+/', ' ', $data);

For removing new lines:

$string = preg_replace('/\r\n/', "", $data);
Druid
  • 6,423
  • 4
  • 41
  • 56
Indian
  • 645
  • 7
  • 22