Sharing of commonly used PHP operation functions
1. PHP encryption and decryption
PHP encryption and decryption functions can be used to encrypt some useful strings and store them in the database, and through reversible decryption of the strings, the The function uses base64 and MD5 encryption and decryption.
1 2 3 4 5 6 7 8 9 |
|
The usage method is as follows:
1 2 |
|
2. PHP generates random strings
When we need to generate a random name, temporary password and other strings, we can use the following Function:
1 2 3 4 5 6 7 8 |
|
The usage method is as follows:
1 |
|
3. PHP gets the file extension (suffix)
The following function can quickly get the file extension, that is, the suffix.
1 2 3 4 |
|
The usage method is as follows:
1 2 |
|
4. PHP gets the file size and formats it
The function used below can get the file size and convert it into KB that is easy to read. , MB and other formats.
1 2 3 4 5 6 7 8 |
|
The usage method is as follows:
1 2 |
|
5. PHP replace tag characters
Sometimes we need to replace strings and template tags with specified content, you can use the following Function:
1 2 3 4 |
|
The usage is as follows:
1 2 3 4 |
|
6. PHP lists the file names in the directory
If you want to list all the files in the directory, use the following code That’s it:
1 2 3 4 5 6 7 8 9 10 |
|
The usage is as follows:
1 |
|
7. PHP gets the URL of the current page
The following function can get the URL of the current page, whether it is http or https:
1 2 3 4 5 6 7 8 9 10 11 |
|
The usage method is as follows:
1 |
|
8. PHP forced download of files
Sometimes we don’t want the browser to directly open files, such as PDF files, but to download files directly, Then the following function can force the file to be downloaded. The application/octet-stream header type is used in the function.
1 2 3 4 5 6 7 8 9 10 |
|
The usage method is as follows:
1 |
|
9. PHP intercepts the string length
We often encounter situations where we need to intercept the length of a string (including Chinese characters), such as The title cannot exceed how many characters. The excess length is represented by.... The following function can meet your needs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
The usage method is as follows:
1 2 |
|
10. PHP gets the real IP of the client
We often use the database to record the user’s IP. The following code can get the real IP of the client. IP:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
The usage method is as follows:
1 |
|
11. PHP prevents SQL injection
When we query the database, for security reasons, we need to filter some illegal characters to prevent SQL For malicious injection, please take a look at the function:
1 2 3 4 5 6 7 8 9 |
|
The usage method is as follows:
1 |
|
12. PHP page prompts and jumps
When we perform form operations, sometimes in order to be friendly If you need to prompt the user for the operation result and jump to the relevant page, please see the following function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
The usage is as follows:
1 |
|
13. PHP calculation time
We are processing it time, it is necessary to calculate the length of time from the current time to a certain point in time. For example, to calculate the running time of the client, hh:mm:ss is usually used to represent
1 2 3 4 5 6 7 8 9 10 |
|
The usage method is as follows:
1 2 |
|
The following is Obtain client IP, string interception, download, etc. For details, please view the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
|
Related recommendations:
Summary of string operation functions in php
php delete folder operation function and several methods example code summary
php summary of commonly used string operation functions
The above is the detailed content of Sharing of commonly used PHP operation functions. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
