0

I am not a great regex specialist, but I know here are some ...

Here's the challenge:

1.) Use-Case: I want to convert strings into arrays using the PHP function parse_str().

To help make "input" better readeable, I want to allow for spaces, tabs and newlines (whitespaces) wherever posible. That shall be "cleaned" out, if NOT in a value or in a key-name.

As whitespaces should be allowed in the data itself (and maybe WITHIN keys), I cannot just strip all whitespaces before parse_str() ...

Of course I could iterate the array after parse_str and use trim() for the keys and values, but I hope that a preg_replace would do that more efficient (and therefor faster)?

2.) Search: Now, I search for a regex (to be used in PHP's preg_replace ) that eliminates: 2a.) all whitespace between the last non-whitespace char and the "&" which defines "next pair" 2b.) all whitespaces between "&" and the next non-whitespace char 2c.) all whitespace before and after "="

Is that possible? And: Did I overlook something?

Thanks in advance

5
  • 1
    Did I overlook something? - Sample inputs, expected outputs, and what you have tried and what is not working. Jun 2, 2016 at 10:24
  • We are not here to solve problems or write code for people. What have you tried so far, and what exactly do you need help with? Jun 2, 2016 at 10:27
  • 1
    @SverriM.Olsen Actually that's exactly why I'm here. That, however, doesn't mean that I/we don't need the info you and Wiktor asks for ;)
    – SamWhan
    Jun 2, 2016 at 10:34
  • Really, it can be \s*(=)\s*|(\S)\s+(&)|(&)\s+(\S) -> $1$2$3$4$5, or \s*(=)\s*|(?<=\S)\s+(?=&)|(?<=&)\s+(?=\S) -> $1, etc... We do not have the data, only you have it, and until we can test our suggestions, your question is just unclear. Jun 2, 2016 at 10:36
  • ok. sorry I did not add examples, thought that my question was clear enough. Some examples: A_B_C = tx charfollowed &G= charfollowed & U=xyz &O=rrr shall go into: A_B_C = tx charfollowed&G= charfollowed&U=xyz&O=rrr
    – jwka
    Jun 2, 2016 at 14:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.