views:

38

answers:

2

Hi, Im quite new to stackoverflow so I dont know if this question has been asked before, but I cant seem to find any past questions which hint at the answer. Any help is really appreciated and thanks in advance.

I have this text:

{
   1282837200, -- [1]
   "Type", -- [2]
   "Name", -- [3]
   "Reason", -- [4]
   Amount, -- [5]
  }, -- [1]
  {
   1282838220, -- [1]
   "Type", -- [2]
   "Name", -- [3]
   "Reason", -- [4]
   Amount, -- [5]
  }, -- [2]

Which I need to create a regular expression for and extract the necessary data. I understand how to extract data from source files using regular expressions within PHP, using source text which has no "whitespace" or carriage returns.

But I need help with creating an expression which includes the whitespace etc. included within this source.

A: 

\s is the symbol for whitespace, which may or may not include newlines, anyway there's [\r\n] for them, ignore these and that's it. You could also tell what exactly you want to do or even post the regex you have so far so I can help you further if you want.

EDIT: I'm just guessing, the regexp you want may be this:

/\s*{[\s\n\r]*^\s*(\d+).*[\s\n\r]*^\s*"([^"]+)".*[\s\n\r]*^\s*"([^"]+)".*[\s\n\r]*^\s*"([^"]+)".*[\s\n\r]*^\s*(\w+).*[\s\n\r]*^\s*},.*[\s\n\r]*^/m

This matches one block and disregards the numbers in brackers. /m switch triggers multiline mode.

Raveren
A: 

If you're trying to simply remove all whitespace, try the following: $string = preg_replace('/\s+/s', '', $string);.

\s+ matches one or more spaces, tabs, and newlines. The s in /s tells the regex to look at the string as a whole as opposed to one line at a time.

Tim