How To Convert URL encoded String To PHP Array Tutorial

Share this Article
Reading Time: 3 minutes
373 Views

Inside this article we will see the concept i.e How To Convert URL encoded String To PHP Array Tutorial. Article contains the classified information about php how to get array from url encoded string.

If you are looking for a solution i.e how to get array from URL encoded string value then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.

Example (URL encoded value)

first_name=Sanjay+Kumar&email=sanjay.onlinewebtutor%40gmail.com&form-pageid=418496&phone=1122334455&comment=Sample+message&formId=contact_form_8540374&formplaceholder=first_name;;email;;form-pageid;;phone;;comment;;

Learn More –

Let’s get started.

Application Programming

Create a folder say url-decode in your localhost directory. Create a file index.php into it.

Open index.php file and write this code into it.

<?php

function getUserFormData($postVal)
{
    $formParamsArr = explode("&", $postVal);
    $submittedData = array_map(function ($item) {
        $itemdata = explode("=", $item);
        return array(
            $itemdata[0] => $itemdata[1]
        );
    }, $formParamsArr);

    $formdata = [];

    foreach ($submittedData as $form => $dataArr) {

        foreach ($dataArr as $index => $value) {

            $formdata[$index] = urldecode($value);
        }
    }
    return $formdata;
}

$formdata = getUserFormData("first_name=Sanjay+Kumar&email=sanjay.onlinewebtutor%40gmail.com&form-pageid=418496&phone=1122334455&comment=Sample+message&formId=contact_form_8540374&formplaceholder=first_name;;email;;form-pageid;;phone;;comment;;");

echo "<pre>";
print_r($formdata);

getUserFormData() function converts URL encoded string to PHP array.

You need to use a PHP function to remove encoded characters from string i.e urldecode(). If you don’t use this function then, you will see encoded characters into value.

But, we have used in our custom function. Let’s run that.

Application Testing

Now,

Open project into browser.

URL: http://localhost/url-decode/index.php

We hope this article helped you to learn How To Convert URL encoded String To PHP Array Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5