1

Is it possible to use in explode/implode functions delimiters with ASCII code less than 32?

I mean something like

$v = explode(chr(29), $string)

or

$v = explode(chr(8), $string)

I've trying severe ways but I have always an error

implode(): Invalid arguments passed

How can I do it, if it's possible?

I'm working in a PHP rotine with a request to an URL: http://192.168.xxx.xxx/?city=22&street=11 that returns a string in HTML with delimiters.

Something like name=John&age=32&gender=male but with chr(9) and chr(31) in '=' and '&' places. How can I explode with chr(9) ou chr(31), for example?

I want that those delimiters will be in char code less that 32, to avoid errors with characters that can be obtained in keyboard, so the idea is to use char codes less chr(32).

Avinash Dalvi
  • 8,551
  • 7
  • 27
  • 53
  • 1
    Can you post an actual example which reproduces your error so others can test it. – Nigel Ren Dec 15 '19 at 17:30
  • You may want to look into urlencoding the data instead - https://stackoverflow.com/questions/4744888/how-to-properly-url-encode-a-string-in-php. – Nigel Ren Dec 15 '19 at 17:47

1 Answers1

0

I think I found the solution...

function str2array($orig = null)
    {
    $arr = $dta = [];
    $val = null;
    if(ord(substr($orig, -1)) != 31) $orig .= chr(31);
    while(strlen($orig))
        {
        $byte = $orig[0];
        if(ord($byte) == 8)
            {
            $dta[0] = $val;
            $val = null;
            }
        elseif(ord($byte) == 31)
            {
            $dta[1] = $val;
            $arr[$dta[0]] = $dta[1];
            $val = null;
            $dta = [];
            }
        else $val .= $byte;
        $orig = substr($orig, 1);
        };
    return $arr;
    };