Home  >  Article  >  Backend Development  >  18 commonly used string functions in PHP

18 commonly used string functions in PHP

山海
山海forward
2019-09-19 14:16:394179browse

String functions are an indispensable part of the basic operations of PHP and one of the most important parts. This article lists and introduces more than a dozen commonly used string operations. I hope it will be helpful to everyone.

18 commonly used string functions in PHP

String formatting

String truncation:

trim():

Remove characters (carriage return, line feed, tab) at the beginning and end of the string and return the result string

At the same time, the trim() parameter can also set a custom special character filter list

rtrim():

Delete special characters starting from the beginning of the string Characters

ltrim():

Delete special characters starting from the end of the string

chop():

Similar to the rtrim() function

Formatted output:

htmlspecialchars(): Function filtering Output

htmlspecialchars():The function will output a string as an HTML entity (characters with special meanings in the string can be translated into HTML entities)

str_replace(): Regular filter output

nl2br():HTML formatting

nl2br(): The function takes a string as an input operation, use The < br /> tag in HTML replaces the \n symbol in a string; this is useful for displaying a long string on an HTML browser.

Character formatted output: sprintf()/printf()

In character formatted output, use the % character format output control character to normalize characters

String case

strtoupper(): String case

strtolower( ): String lowercase

ucfirst(): Capitalize the first letter

##ucwords(): Change the first character of each English paragraph Upper case

String concatenation and splitting

explode(): Use the specified character as the splitting condition and return to An array

strtok(): Decompose a string into a set of strings

The delimiter can be a character or a string, and the strtok() function each time Only one substring will be read based on the delimiter;

substr():

returns a string consisting of the given starting point and end point. New string result

string substr(string string, int start [,int length]);

The function returns the string content in the string string according to the start and length constraints

String comparison

Sort

strcmp(): Compare two strings

strcasecmp(): Compare strings (not case sensitive)

strnatcmp(): Natural Sorting (not case sensitive)

strlen() : Determine the length of the string

Character matching

String search

strstr(): Match the search character or string

strchr(): Match and search for a character

strstr() and strchr() are equally effective in matching and searching applications; the function requires more than two parameters, the first parameter is: needs to be Search string, the second parameter is: the target keyword to be searched; matching the string to the keyword will return the searched string, otherwise return false

characters String position :

strpos(): Returns the position of the matched character

strpos(): Parameters of the function Similar to strstr(), the third parameter of strpos() can specify the position to start searching

String replacement:

str_replace(): String replaces other characters in the string.

The above is the detailed content of 18 commonly used string functions in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete