Chapter 8 string processing

Source: Internet
Author: User
Tags chop strtok

Note: This article is a video tutorial from Li Yanhui PHP. This article is intended for communication purposes only and cannot be used for commercial purposes. Otherwise, the consequences will be borne by you.

Learning points:
1. String formatting
2. Operation substring
3. String comparison
4. Search for replacement strings
5. Process Chinese Characters

 

In daily programming, processing, adjusting, and finally controlling strings are very important.
This is the foundation of all programming languages. Different from other languages, PHP is not so troublesome to use data types to process words.
String. In this way, string processing in PHP is no longer easy.

 

1. String formatting

The first step to sort strings is to clear unnecessary spaces in strings. Although this operation is not required, if
It is very useful to store a string in a file or database, or compare it with other strings.
The chop () function removes unnecessary white spaces after the string, including new rows.
The ltrim () function removes unnecessary spaces from the start of a string.
The rtrim () function removes unnecessary spaces behind the string, including new rows. This function is the alias of chop.
The trim () function removes unnecessary spaces on both sides of the string.

<? ('        PHP       '?>

PHP has a series of functions available to reformat strings. These functions work in different ways.
.
The nl2br () function uses a string as the input parameter and uses the <br/> mark in XHTML to replace the line break in the string.

<? ("This is a Teacher!\nThis is a Student!"?>

Convert special characters to HTML equivalent form. You can use the htmlentities () and htmlspecialchars functions.
If you want to remove HTML from the string, you can use the strip_tags () function.

<? ('<Strong> I'm Wu Yu! </Strong> '); (' <strong> I'm Wu Yu! </Strong> ') (' <strong> I'm Wu Yu! </Strong> ')?>

For strings, some characters must be valid, but when data is inserted into the database, it may
This causes some problems because the database interprets these characters as controllers. These problematic characters are quotation marks (single quotes
And double quotes), backslash (\), and NULL characters.
PHP provides two functions specifically used to escape strings. Before writing any string to the database, you should
Use addslashes () to reformat them,
After addslashes () is called, slashes are added to all quotation marks, while the stripslashes () function removes these slashes.

<? ('This is \a" Teacher! '?>

You can reformat the uppercase and lowercase letters in a string.
The strtoupper () function converts a string to uppercase.
The strtolower () function converts a string to lowercase.
The ucfirst () function converts the first letter into uppercase letters.
Ucwords () function converts the first letter of each word to uppercase

<? ('yc60.com@gmail.com'?>

String filling function: str_pad () fills a string with a specified number of characters.

<? ('Salad',10).'is good.'?>

Ii. Operation substring

Generally, we want to view each part of the string. For example, you can view words in a sentence or
Sub-mail addresses are divided into component parts. PHP provides several string functions to implement this function.
Use functions such as explode (), implode (), and join ()
To implement this function, the first function we will use is explode ().
Use implode () or join () functions to achieve the opposite effect of the function explode (). The effects of these two functions are one.
.

<? = 'yc60.com@gmail.com' = ('@',?>

Use the strtok () function
The strtok () function extracts only some fragments (called tokens) from the string at a time ). For
For word processing, the strtok () function is better than the explode () function.

<? = "I,will.be#back" = (,",.#"( "<br \>" = (",.#"?>

Use the substr () function
The substr () function allows us to access a substring of the given start and end points of a string. This function is not applicable.
In our example, however, it is useful when you need to obtain a part of a fixed format string.

<? ("abcdef", 1, 3?>

Returns an array. Each array element is a character in the string parameter.
String.

<?(('This is a Teacher!'?>

Reverse string: strrev () can reverse a string.

<? ('This is a Teacher!'?>

3. String comparison

So far, we have used "=" to compare whether two strings are equal. You can use PHP to perform step 1.
More complex comparisons. These comparisons are divided into two types: partial matching and other cases.
Sort strings: strcmp (), strcasecmp (), and strnatcmp ()
This function requires two parameter strings for comparison. If the two strings are equal, the function returns 0. If
Returns a positive number after the Lexicographic Order str1 and str2 (greater than str2). If str1 is less than str2, a negative value is returned.
Number. This function is case sensitive.
The strcasecmp () function is the same as strcmp () except case insensitive.
The strnatcmp () function and its corresponding case-insensitive strnatcasecmp () function are added in PHP4.
These two functions compare strings by "natural sorting". The so-called natural sorting is to sort strings in the order that people prefer.

<? ('a','b'?>

Returns the length of the first part of a string that contains the characters in another string using the strspn () function. Also
Is to find the same part between two strings.

<? ('gmail','yc60.com@gmail.com'?>

Use the strlen () function to test the string length.
You can use the strlen () function to check the length of a string. If a string is passed to it, this function returns
The length of the string. For example, strlen ("hello") returns 5.

<? ('This is a Teacher!'?>

Determine the frequency of occurrence of a string: substr_count () returns the number of occurrences of a string in another string.

<? ('yc60.com@gmail.com','c'?>

4. Search for replacement strings

Generally, we need to check whether a longer string contains a specific substring. This partial match
It is generally more useful than the test string's full equivalence.
Search for strings: strstr (), strchr (), strrchr (), and stristr ()
Strstr () is the most common function. It can be used to search for matched strings or words in a long string.
. Note that the strchr () and strstr () functions are identical.

<? ('yc60.com@gmail.com','@'?>

The strstr () function has two variants. The first variant is stristr (), which is almost the same as strstr (). The difference is that
The delimiter size. This function is very useful for form-only applications because users can enter
"Delivery", "Delivery", and "DELIVERY ".
The second variant is strrchr (), which is almost the same as strstr (), but only the alias of strstr.
String location: strpos () and strrpos ().
The operations of strpos () and strrpos () functions are similar to those of strstr (). However, instead of returning a substring, strpos () returns a substring.
Position of the string needle in the string haystack. More interestingly, strpos () is recommended in the current PHP manual ()
The function replaces the strstr () function to view the position of a substring in a string because of its running speed.

Faster.

<? ('yc60.com@gmail.com','c'?>

Replacement string: str_replace (), str_ireplace (), substr_replace ()

<? ('@','#','yc60.com@gmail.com' ('yc60.com@gmail.com','###',0,5?>

5. Process Chinese Characters

Some of the above string functions can be used in Chinese, but some do not apply in Chinese. Therefore, PHP provides
To solve this problem.
The Chinese characters can be gbk, utf8, or gb2312.
The function corresponding to mb_strlen () is strlen () to evaluate the length of the string.
The function corresponding to mb_strstr () is strstr () to evaluate the character string to the end.
The function corresponding to mb_strpos () is strpos ().
The function corresponding to mb_substr () is substr () to retrieve the specified string.
The corresponding function of mb_substr_count () is substr_str (). The number of times a string is returned.

Scan the help manual

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.