Summary of common string formatting functions in PHP

Source: Internet
Author: User
Tags rtrim
This article mainly introduces the summary of common string formatting functions in PHP. The functions described in this article are frequently used on the WEB, for example, the function of filling spaces and strings, converting strings in case, and HTML Tag-related words are extracted.

This article mainly introduces the summary of common string formatting functions in PHP. The functions described in this article are frequently used on the WEB, for example, the function of filling spaces and strings, converting strings in case, and HTML Tag-related words are extracted.

String formatting refers to processing a string into a specific format. Generally, the data submitted to the server from the form is in the string format. To achieve the expected output, you need to process these strings in a certain format before using them. Shows the common string formatting functions:

Note: Most of the strings processed by string functions provided in PHP are not modified on the original string, but return a formatted new string.

1. Remove spaces and strings to fill the Function

A space is also a valid character, which occupies a position in the string. When users input data in a form, they often accidentally enter meaningless spaces. Therefore, when the PHP script receives data processed through a form, it first processes unnecessary spaces in the string or other meaningless symbols. In PHP, you can use the ltrim (), rtrim (), and trim () functions to do this. The syntax format of the three functions is the same, but the functions are different. Their syntax format is as follows:

The Code is as follows:


String ltrim (string str [, string charlist]) // remove spaces or other predefined characters from the left of the string
String rtrim (string str [, string charlist]) // removes white spaces or other predefined characters from the right of the string
String trim (string str [, string charlist]) // removes white spaces or other predefined characters from both ends of the string.


These three functions are used to delete white spaces or other predefined characters from the left, right, and both ends of the string. The processed results will be returned in the form of a new string and will not be modified on the original string. The first parameter str is a string to be processed and is required. The second charlist parameter is a filter string used to specify the special symbols to be removed. This parameter is optional. If no filter string is specified, the following characters are removed by default.

★"": Space
★"0 \": NULL
★"\ T": Tab
★"\ N": New Line
★"\ R": Enter

You can also use "symbol specifies a range to be removed, for example," 0 .. 9 "or" .. z "indicates removing digits and small letters from ASCII code values. Their usage code is as follows:

The Code is as follows:


<? Php
$ Str = "123 This is a test..."; // declare a test string starting with a number on the left and a ellipsis on the right
Echo ltrim ($ str, "0 .. 9"); // filter out the number on the left of the string and output This is a test...
Echo rtrim ($ str, ".") // filter out all "." on the right of the string, and output: 123 This is a test
Echo trim ($ str, "0 .. 9 .. z. "); // filter out the numbers and uppercase letters at both ends of the string and". ", output: his is a test
?>

Not only can the content in the string be filtered out as needed, but also the str_pad () function can be used to fill the string as needed. It can be used to protect sensitive information, such as data pairs and sorting. The function prototype is as follows:

The Code is as follows:


String str_pad (string input, int pad_length [, string pad_string [, int pad_type])


This function has four parameters. The first parameter specifies the string to be processed. The second parameter is the length of the processed string. If the value is smaller than the length of the original string, no operation is performed. The third parameter specifies the string used for filling. It is an optional parameter. If not specified, spaces are used by default. The last parameter specifies the fill direction. It has three optional values: STR_PAD_BOTH, STR_PAD_LEFT, and STR_PAD_RIGHT, which indicate filling at both ends, left, and right of the string respectively. It is also an optional parameter. If not specified, the default value is STR_PAD_RIGHT. The code for using the str_pad () function is as follows:

The Code is as follows:


<? Php
$ Str = "LAMP ";
Echo str_pad ($ str, 10); // specify the length to 10. By default, "LAMP" is filled with spaces on the right"
Echo str_pad ($ str, 10, "-=" STR_PAD_LEFT); // specify the length to 10 and fill "-= LAMP" on the left"
Echo str_pad ($ str, 10, "_" STR_PAD_BOTH); // specify the length to 10 and fill "___ LAMP ___" on the left ___"
?>

Ii. String case-insensitive Conversion

Four string case-sensitivity conversion functions are provided in PHP. They all have only one optional parameter, that is, the string to be converted. You can directly use these functions to perform case-insensitive conversion. The strtoupper () function is used to convert all the given strings to uppercase letters. The strtolower () function is used to convert all the given strings to lowercase letters. The ucfirst () function () converts the first letter of a given string to uppercase, and the rest of the characters remain unchanged. ucwords () is used to convert the first letter of all words in a given string separated by spaces to uppercase. The following code is used for these functions:

The Code is as follows:


<? Php
$ Lamp = "lamp is composed of Linux, Apache, MySQL and PHP ";
Echo strtolower ($ lamp); // output: lamp is composed of linux, apache, mysql and php
Echo strtoupper ($ lamp); // output: lamp is conposed of linux, APACHE, MYSQL AND PHP
Echo ucfirst ($ lamp); // output: Lamp is composed of Linux, Apache, MySQL and PHP
Echo ucwords ($ lamp); // output: Lamp Is Composed Of Linux, Apache, MySQL And PHP
?>


These functions only work in the way they describe. To ensure that the first letter of a string is an upper-case letter, while the rest are lower-case letters, you need to use the appropriate method. As follows:

The Code is as follows:


<? Php
$ Lamp = "lamp is composed of Linux, Apache, MySQL and PHP ";
Echo ucfirst (strtolower ($ lamp); // output: Lamp is composed of linux, apache, mysql and php
?>

3. HTML tag-related string formatting

Adding resources to HTML input forms and URLs is a way for users to submit data to the server. If the data cannot be well processed, it may become the entry point for hackers to attack the server. For example, when a user publishes an article, if the article contains code such as HTML format tags or JavaScript page turns, the layout of the page will be changed if the code is output directly. Because the code is sent to the browser, the browser will explain it according to the valid code. Therefore, in the PHP script, the data content submitted by the user must be processed first. In PHP, we provide comprehensive HTML-related string formatting functions to effectively control HTML text output.

① Function nl2br ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.