-1

I want to split the parameters sent through URL. Here is my text.

"mytext=A & B Company&anothertext=20&texttwo=SampleText&array[1]=10&array[2]=20"

Expected output:

['mytext'=>'A & B Company', 'anothertext'=> 20, 'texttwo' => 'SampleText', array[1] => 10, array[2] => 20 ].

I tried with explode('&', $params) and parse_str($url_components['query'], $params);

both giving only A as result. I need as 'A & B Company'. How to achieve this?

Saravanan DS
  • 278
  • 3
  • 14
  • Does this answer your question? [How can I get parameters from a URL string?](https://stackoverflow.com/questions/11480763/how-can-i-get-parameters-from-a-url-string) – IROEGBU Oct 27 '22 at 15:21
  • The `A & B Company` is the real problem.... – KIKO Software Oct 27 '22 at 15:23
  • You either need to url encode the values (specifically the `&` in `A & B Company`), or you need to write your own parsing for it. No non-custom PHP function will some how understand which `&` you want it to treat as values and which to treat as delimiters. – M. Eriksson Oct 27 '22 at 15:23
  • 1
    your original text is already wrongly encoded. is it supposed to be url-encoded? because in url-encoding it should have been `mytext=A+%26+B+Company&anothertext=20&texttwo=SampleText&array%5B1%5D=10&array%5B2%5D=20` – hanshenrik Oct 27 '22 at 15:32

1 Answers1

0

I think you're looking for parse_str()?

See: https://3v4l.org/L2UZa

The result is:

array(5) {
  ["mytext"]     => string(2) "A "
  ["B_Company"]  => string(0) ""
  ["anothertext"]=> string(2) "20"
  ["texttwo"]    => string(10) "SampleText"
  ["array"]      => array(2) {
    [1] => string(2) "10"
    [2] => string(2) "20"
  }
}

This differs slight from what you want because of the &. if you could replace the & with the URL encoded version %26 it would work.

See: https://3v4l.org/OXfsD

The result now is:

array(5) {
  ["mytext"]     => string(2) "A & B_Company"
  ["anothertext"]=> string(2) "20"
  ["texttwo"]    => string(10) "SampleText"
  ["array"]      => array(2) {
    [1] => string(2) "10"
    [2] => string(2) "20"
  }
}

Basically the & is an anomaly, it shouldn't have been there in the first place. URL query parameters should be made with urlencode(), or something equivalent, which would have replace & by %26.

KIKO Software
  • 15,283
  • 3
  • 18
  • 33
  • 1
    I need mytext as `A & B Company` not as `A`. – Saravanan DS Oct 27 '22 at 15:23
  • @SaravananDS I agree. Would it be possible to replace the `&` with `%26`? That's the URL encoded version. – KIKO Software Oct 27 '22 at 15:25
  • idk what format his string is, but url-encoded isn't it. looks like he got a string that is SUPPOSED to be url-encoded, but either has been encoded incorrectly (a bugged encoder) or decoded incorrectly (a bugged decoder) - either way, there's a bug higher up in the code. if it was correctly url-encoded it should have been ```mytext=A+%26+B+Company&anothertext=20&texttwo=SampleText&array%5B1%5D=10&array%5B2%5D=20``` – hanshenrik Oct 27 '22 at 15:34
  • 2
    @hanshenrik That's exactly what I was trying to say, although I first parsed it and then discovered the weirdness. Either way, if it is meant to be URL-encoded, the source of the string should be changed, instead of trying to make a complex parser for it that will ultimately always fail. – KIKO Software Oct 27 '22 at 15:38
  • @hanshenrik, It helped. Now my text is encoded and able to get entire text as expected. – Saravanan DS Oct 28 '22 at 02:11