Home  >  Article  >  Backend Development  >  How to convert string or binary data to Base64 encoding in php

How to convert string or binary data to Base64 encoding in php

PHPz
PHPzOriginal
2023-04-12 09:17:561734browse

In modern Web development, there are many ways to transmit data, among which Base64 encoding has become a common encoding method. In PHP programming, encoding string or binary data into Base64 encoding is also very simple.

In this article, we will introduce how to convert string or binary data to Base64 encoding in PHP, and how to decode Base64 encoding. We will also discuss some practical uses of Base64 encoding.

1. What is Base64 encoding?

Base64 is a 64-character encoding method used to convert binary data into plain text format for easy use in network transmission or storage. The Base64 encoding table includes uppercase and lowercase letters, numbers, and some special characters, a total of 64 characters. As shown below:

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 /

In order to convert any binary data into readable ASCII code characters, Base64 encoding packs every three bytes into a group, and then these three Bytes are converted into a 64-character encoding format, ultimately generating a string composed of readable ASCII characters. If the number of bytes of data is not a multiple of 3, Base64 encoding adds some special characters at the end to make the entire encoding length a multiple of 4.

2. How to convert string and binary data into Base64 encoding in PHP?

Converting string and binary data to Base64 encoding in PHP is very simple. PHP provides a function base64_encode(), which can encode actual parameters into Base64 encoding.

The following is an example of encoding a string into Base64 encoding:

<?php
$str = "Hello World!";
$encoded_str = base64_encode($str);
echo $encoded_str;
?>

When the above code is executed, the output result is:

SGVsbG8gV29ybGQh

The above output result is Base64 encoding of the "Hello World!" string.

To encode binary data into Base64 encoding, just pass in the binary data as a parameter of the base64_encode() function.

The following is an example of encoding binary data into Base64 encoding:

<?php
$data = file_get_contents("image.jpg");
$encoded_data = base64_encode($data);
echo $encoded_data;
?>

When the above code is executed, the output result is Base64 encoding after encoding the binary data in the image.jpg file . In the above example, in order to read the data in the file into a string, we used the file_get_contents() function in PHP.

3. How to decode Base64 encoding in PHP?

Decoding Base64 encoding to raw data in PHP is also very simple. PHP provides a function base64_decode(), which can decode a Base64-encoded string into raw data.

The following is an example of decoding a Base64-encoded string into a string:

<?php
$encoded_str = "SGVsbG8gV29ybGQh";
$str = base64_decode($encoded_str);
echo $str;
?>

When the above code is executed, the output result is the string "Hello World!", which string That is the original data encoded by Base64 above.

For decoding Base64 encoding into binary data, the method is similar to the above example.

4. Some uses of Base64 encoding

In Web development, Base64 encoding has many applications. Here we briefly introduce its two common applications.

  1. Display of pictures or other binary data in Web pages

In Web development, pictures or other binary data need to be displayed to users through web pages. If binary data is used directly, many unpredictable problems will occur. For example, binary data may be truncated during transmission or cannot be parsed correctly. Base64 encoding can convert binary data into text format for easy display on the Web.

The following is an example of converting an image into Base64 encoding.

<?php
$data = file_get_contents("image.jpg");
$encoded_data = base64_encode($data);
echo "<img src=&#39;data:image/jpeg;base64," . $encoded_data . "&#39;/>";
?>

When the above code is executed, the output will display an img tag containing image.jpg image data.

  1. Application in API transmission

In modern applications, API interfaces are used for data transmission between different platforms. In API transmission, the size and type of data need to take into account the security and efficiency of data transmission. Base64 encoding can convert binary data into text format for easy use in API transmission.

Summary

In PHP development, converting string and binary data into Base64 encoding is very simple. PHP provides base64_encode() and base64_decode() functions to perform this operation. Base64 encoding has many applications in web development, such as displaying images or other binary data in web pages, and in API transmission for securely transmitting data.

The above is the detailed content of How to convert string or binary data to Base64 encoding in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn