Urlencode function in php

URL Encoding and Decoding with PHP

PHP provides us with the ability to encode and decode URLs with the implementation of two main functions. However, the question is, why is it used? Encoding and decoding URL strings are used to convert general URL strings and characters into an arrangement that can be conveyed over the internet. In this tutorial, you will learn about two ways in which URL string can be encoded and decoded in PHP.

URL Encoding with PHP

Encoding plays an important role in different scenarios of technology and programming. URL encoding is used in PHP to convert the URL by including typical entities or characters into it. Usually, when an URL holds non-alphanumeric characters, these characters will be encoded, which will replace these characters with some specific encoding entities. However, some distinct exceptional characters cannot be replaced.

Such an encoding mechanism is essential to be achieved before sending the URL data to query string or in a function for making it work dynamically on any URL data. Once the work is done on that encoded URL, these encoded data is then the URL is decoded into its original form.

Читайте также:  Python and google app engine

urlencode() function in PHP is used for encoding a string associated with the URL. This function is responsible for encoding in the same manner data posted on the web page is encoded. This function will return an encoded string when executed.

The syntax of using this function is:

Characteristics of URL Encoding

  • While the internet sends the URL, it is only possible if its characters are in the ASCII format.
  • PHP’s encoding schemes will replace the unsafe ASCII characters with a «%» tailed by 2 hex-digit.
  • The URL must be converted to an effective ASCII character-set because URLs often contain characters outside the ASCII set.
  • An URL cannot have any spaces, so these spaces are replaced with either the ‘%20‘ or ‘+‘ (plus) symbol.

The urlencode() Function Example

An example of using this URL encoding function is as follows:

URL Decoding with PHP

Another function called the urldecode() function is another inbuilt function of PHP and is implemented for decoding the URL, encoded by the urlencode() function. Decoding is the approach of reversing the non-ASCII data back to its original form. This function will accept a single string as its parameter. That string will contain the encoded URL to be decoded.

The urldecode() Function Example

An example of using this URL decoding function is as follows:

Types of URL Encoding and Decoding in PHP

RFC 3986 standard type

This is a type of encoding-decoding approach where the PHP functions rawurlencode() and rawurldecode() are implemented to encode and decode the URL, respectively. This is not a part of this tutorial. Here, URL spaces are replaced with %[hex code] rather than the plus (+) symbol.

application/x-www-form-urlencoded type

This is a type of encoding-decoding approach where the built-in PHP functions urlencode() and urldecode()are implemented to encode and decode the URL, respectively. This encoding will replace almost all the special characters other than (_), (-), and (.) in the given URL. Space is also replaced with a (+) plus sign rather than a %[hex code].

Источник

Urlencode function in php

Learn Latest Tutorials

Splunk tutorial

SPSS tutorial

Swagger tutorial

T-SQL tutorial

Tumblr tutorial

React tutorial

Regex tutorial

Reinforcement learning tutorial

R Programming tutorial

RxJS tutorial

React Native tutorial

Python Design Patterns

Python Pillow tutorial

Python Turtle tutorial

Keras tutorial

Preparation

Aptitude

Logical Reasoning

Verbal Ability

Company Interview Questions

Artificial Intelligence

AWS Tutorial

Selenium tutorial

Cloud Computing

Hadoop tutorial

ReactJS Tutorial

Data Science Tutorial

Angular 7 Tutorial

Blockchain Tutorial

Git Tutorial

Machine Learning Tutorial

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures tutorial

DAA tutorial

Operating System

Computer Network tutorial

Compiler Design tutorial

Computer Organization and Architecture

Discrete Mathematics Tutorial

Ethical Hacking

Computer Graphics Tutorial

Software Engineering

html tutorial

Cyber Security tutorial

Automata Tutorial

C Language tutorial

C++ tutorial

Java tutorial

.Net Framework tutorial

Python tutorial

List of Programs

Control Systems tutorial

Data Mining Tutorial

Data Warehouse Tutorial

Javatpoint Services

JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.

  • Website Designing
  • Website Development
  • Java Development
  • PHP Development
  • WordPress
  • Graphic Designing
  • Logo
  • Digital Marketing
  • On Page and Off Page SEO
  • PPC
  • Content Development
  • Corporate Training
  • Classroom and Online Training
  • Data Entry

Training For College Campus

JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week

Like/Subscribe us for latest updates or newsletter RSS Feed Subscribe to Get Email Alerts Facebook Page Twitter Page YouTube Blog Page

Источник

Using PHP urlencode and urldecode

Monty Shokeen

Monty Shokeen Last updated Aug 21, 2021

Every now and then you will have to pass data to a webpage or service using a URL, for example in a GET request. This is pretty easy, since URLs are basically just text strings, but things can sometimes get complicated. For example, some characters have a special meaning in URLs (like : ), and some characters aren’t allowed at all (like space). Sometimes you even need to encode a URL in another URL!

In this tutorial, you’ll learn why you need to encode or decode strings to be passed in URLs and how to use the built-in PHP functions to do so.

Why Encode and Decode Strings to URLs

Perhaps you want to pass some info as query parameters to a web service or another web page. Say, for example, you want to send the following data to a website as a query string:

key data
redirect https://code.tutsplus.com
author monty shokeen
page 2

That information would be encoded in the following query string:

https://www.example.com?redirect=https%3A%2F%2Fcode.tutsplus.com&author=monty%20shokeen&page=2

Notice that the special characters like : and / in the «redirect» URL have been encoded as %3A and %2F to avoid interfering with the structure of the overall URL. This is called escaping, and that’s where the encoding functions come in.

The server at example.com will receive that encoded information in the query string and will probably need to decode it later. That’s where URL decoding is important.

Encoding Strings With urlencode() and rawurlencode()

There are two different functions in PHP for encoding strings in URLs. These are called urlencode() and rawurlencode() . The major difference between these two is the set of characters they encode and how they handle spaces.

In the case of urlencode() , the function replaces all other non-alphanumeric characters except — , _ and . with a percent sign followed by two hex digits. Any spaces are replaced with the + character. On the other hand, the rawurlencode() function replaces all other non-alphanumeric characters except — , _ , . , and ~ with a percent sign followed by two hex digits. This function also replaces spaces with a percent followed by two hex digits: %20 .

The following example should clear things up a bit for you.

Источник

Оцените статью