How To Convert URL encoded String To PHP Array Tutorial

Reading Time: 3 minutes
1,135 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.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

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.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more