PHP simple learning string processing

String processing


1. trim, remove blank characters (or other characters) at the beginning and end of the string

Format: trim (character string name defined by $) is
as follows.
Insert picture description here
If you want to remove a specific character
Format: trim (define character name,'characters you want to omit on the left and right sides') The
example is as follows.
Insert picture description here
You can also remove multiple characters, as follows As
long as there are characters removed in the left and right passes , they can be removed, and will not be affected by the order.
Insert picture description here
Among them, ltrim and rtrim represent the removal of the left and the right respectively, and the basic usage is similar to trim.
2. Strtoupper() string uppercase conversion
Insert picture description here
and the original string is not affected by this code

Insert picture description here
strtolower () lowercase conversion

Insert picture description here
substr_count() finds the number of occurrences of a certain character
and its format: substr_count (define string name,'a character'),
for example, as follows.
Insert picture description here
When changing the format to
substr_count (define string name,'a character', x)
, it will start from the xth Query this character from left to right starting from 0 after the number

Insert picture description here
You can also continue to upload a parameter
substr_count (defining string name,'a character', x, y)
that starts from the xth character, and then y characters are queried, but y cannot be greater than the remaining characters, otherwise an error will be reported.
Insert picture description here
strpos() finds the position of the first occurrence of a string
Format: strpos (the searched string,'searched character')
can search for one or more characters.
If not found, it will return bool type false

Insert picture description here
(If you need to make some judgments, you need to make some special judgments on the judgment conditions)
Because if it is found in the 0th position, it will return 0. This value is converted to Boolean to false, which affects the judgment. Therefore, the judgment should be judged against the string Is the processing congruent? The
following is a correct example.
Insert picture description here
You can add a number (a bit hard to understand)
meaning:Insert picture description here

Format: strpos (the searched string,'searched character', x)
Insert picture description here
strstr finds the first occurrence of a string, and returns the position of the first occurrence to the following string
Format: strstr (defined string, searched character)
Insert picture description here
str_replace () specifies the character to replace the corresponding character
format str_replace ( 'specified character', 'replacement characters' string name)
for example as follows
Insert picture description here
can be uploaded again a parameter
format str_replace ( 'specified character', 'replacement characters' string name, x)
significance : X is the number of times replaced
str_replace There are some functions, I did not record them, until the actual application, the
htmlspecialchars() HTML code is displayed intact on the page
Insert picture description here
(Although I am listening, this function feels good)
strip_tage removes the html and php tags
, that is, removes all the tags about PHP and HTML in the source code.
Because it is used less, I will record it, not in the example
substr-returns a string
format: substr (defined string, parameter )
Strings from this parameter and later will be returned. The
example is as follows.
Insert picture description here
You can upload another parameter, namely the interception length.
Insert picture description here
You can also upload a negative parameter, and the query will be reversed.
explde() string segmentation function
Format explde (separation symbol, defined character)
(the character string contains when the delimiter is fixed)
Insert picture description here
Upload the parameter again, which means that this string is divided into several segments (but not evenly divided), and the last element contains the remaining part.
Insert picture description here
str_split changes the string to the array and uploads the parameter as the number of elements in each array

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_51954912/article/details/114777977