Preg_replace: how?

If there is one thing that I cannot understand (or find out), then this is the syntax preg_replace. I need help in removing all possible characters (space, tab, new line, etc.) Between >and <.

Meaning, I have this XML:

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

I need to see this:

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

So far I have come up with this:

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

But he does not even come close to what I need. The solution will be appreciated.

+5
source share
2 answers

You are close, you just need separators and limit your search to spaces:

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

Where #is the delimiter character, and \sis the abbreviation for any spaces.

You can see that it works in this example .

+7
source

To remove spaces:

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

:

$string = preg_replace('/\r\n/', "", $data);
+1

All Articles