0

I have a textarea where users can put variables like this :

VARIABLE=foo
RANDOM=iloveit
GREAT=amazing

I need to transform this in an array of parameters.

So i did this function (who works) :

public static function cleanWebhookTextarea($textareaLines){

        $textareaLines = preg_split('/(;|,|\r\n,|\r,|\n)/', $textareaLines);
        $params = array();

        foreach ($textareaLines as $line){
            $line = preg_split('/(=)/', $line);
            $params[$line[0]] = $line[1];   
        }

        return $params;

    }

This return me what i want, so :

array:3 [▼
  "VARIABLE" => "foo"
  "RANDOM" => "iloveit"
  "GREAT" => "amazin"
]

But i wondering if there is a quickest way to do what i want ?

Thanks

2
  • This seems like a good way to do it, given that a textbox is required: Newline, semicolon or comma to terminate each statement, = to mark key-value pairs. Alternatively you could create paired input fields for keys and their values. Commented Dec 4, 2019 at 13:06
  • I hope=you've=got=a=**LOT**\r\nof=+error=trapping\tin=there Commented Dec 4, 2019 at 13:10

1 Answer 1

2

I guess you can do it in one line with function like str_replace and parse_str

parse_str(str_replace(PHP_EOL, '&', $data), $result);

Here is a working example.

Sign up to request clarification or add additional context in comments.

1 Comment

Amazing ! I was pretty sure it's possible to optimize. But not expecting so much ! Thanks

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.