Decode a string using PHP's base64_decode() function

王林
Release: 2023-11-03 12:10:01
Original
1189 people have browsed it

Decode a string using PHPs base64_decode() function

In PHP, base64 encoding can be used to convert binary data into printable ASCII characters, thereby facilitating data transmission and storage. The base64_decode() function can decode the base64-encoded string into original binary data. The following will provide specific PHP code examples to introduce how to use this function to decode strings.

First, you can use the following code to base64 encode the string:

$str = "Hello World!";
$base64_str = base64_encode($str); // 编码后的字符串为 "SGVsbG8gV29ybGQh"
Copy after login

In the above code, the variable $str stores the string to be encoded, use base64_encode()After the function encodes, the encoded string is stored in the variable $base64_str. Next, decode using the base64_decode() function:

$decode_str = base64_decode($base64_str); // 解码后的字符串为 "Hello World!"
echo $decode_str;
Copy after login

In the above code, we call the base64_decode() function and pass the encoded string Given to this function, the function returns the decoded original string, stored in the variable $decode_str. Finally, use the echo statement to output the decoded string, and you should be able to see the output of Hello World! on the command line or on the web page.

Of course, in actual operation, we can also base64 encode and decode files, such as the following example:

$file = "example.jpg";
$base64_file = base64_encode(file_get_contents($file));
$decode_file = base64_decode($base64_file);
file_put_contents("new_example.jpg", $decode_file);
Copy after login

In the above code, we use file_get_contents()The function reads the contents of the file named example.jpg, then encodes the read content using the base64_encode() function, and stores the encoded string in Variable $base64_file. Next, use the base64_decode() function to decode, and use the file_put_contents() function to write the decoded binary data to a file named new_example.jpg middle.

To sum up, using PHP for base64 encoding and decoding is a very practical technical means. We can master the basic usage through the above code examples, so that we can work more efficiently in the actual development process.

The above is the detailed content of Decode a string using PHP's base64_decode() function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!