Home > Backend Development > PHP Tutorial > Discuss the summary analysis of various PHP string functions_PHP tutorial

Discuss the summary analysis of various PHP string functions_PHP tutorial

WBOY
Release: 2016-07-21 15:09:31
Original
818 people have browsed it

PHP string functions include find character position functions; extract sub-character functions; replace strings; character length; compare character functions; split into array characters; remove spaces, etc.

The string functions in PHP language are also relatively easy to understand. Today we have summarized nearly 12 PHP string functions for you. We hope it will be helpful to friends who need them again and increase the PHP knowledge base of readers and friends.


1 Search character position function:
strpos($str,search,[int]): Find the search position in $str A position starts from int;
stripos($str,search,[int]):The function returns the position of the first occurrence of a string in another string. This function is not case sensitivestrrpos($str,search,[int]):Find the last occurrence of search in $str starting from int
strripos($str ,search,[int]):Same as above, this function is not case sensitive

2. Extract substring function (double byte)
substr($str,int start[,int length]):From the strat position in $str Start extracting [length length string].
strstr($str1,$str2):Search $str2 from $str1 (the first position) and intercept it from the beginning to the end of the string; if not, return FALSE.
stristr() The function is the same as strstr, but it is not case-sensitive.
strrchr() Returns from the last searched character; Use: Get the file name in the path

3. PHP string function for replacing strings
str_replace(search,replace,$str):Find search from $str and replace with replace
str_ireplace(search,replace,$str):Same as above, this function is not case sensitive
strtr($str,search,replace):replace cannot be used in this function is "";
substr_replace($Str,$rep,$start[,length]):$str original string, $rep replaced new string, $start starting position, The length of $length replacement, this item is optional

4. Character length
int strlen($str)

5. Comparison character function
int strcmp($str1,$str2):$str1>=<$str2 are positive 1,0,-1 respectively (String comparison)
strcasecmp() Same as above (case-insensitive)
strnatcmp("4","14") Compare strings in natural order
strnatcasecmp() Same as above, (case sensitive)

6. PHP string function split into arrays
str_split($str,len):Split $str according to the length of len and return the array
split(search,$str[,int]):Split $str according to search characters and return the array int. How many times will it be split? The rest will not be split.expload(search,$str[,int ])

7. Remove spaces:
ltrim(), rtrim(), trim()

8. Add space function
chunk_split($str,2):Press 2 characters into the $str character to add a space;

9, chr, ord--return the specified character or ascii

10. Functions related to HTML code
nl2br(): convert n to
.
strip_tags($str[,'

']): Remove HTML and PHP tags

All HTML and PHP codes in $str will be removed. The optional parameters are html and PHP codes. The function of the optional parameters is to retain the code written by the optional parameters.
For example: echo strip_tags($text, '

');
htmlspecialchars($str[,parameter]):The page normally outputs HTML code parameters, yes Conversion method

11. PHP string function for character case conversion
strtolower($str): Convert string to lowercase
strtoupper($str ): Convert string to uppercase
ucfirst($str): Convert the first character of the function to uppercase
ucwords($str): Convert the first letter of each word to uppercase

12. Database-related PHP string functions
addslashes($str): Enable single quotes ('), double quotes ("), and backslashes in str Line() with NUL
string converted to ',",\.
magic_quotes_gpc = On: Automatically escape the contents of get, post, and cookie
get_magic_quotes_gpc(): Detect whether magic_quotes_gpc is turned on
stripslashes( ): Remove backslashes from strings

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327300.htmlTechArticlePHP string functions include find character position functions; extract sub-character functions; replace strings; character length; compare characters Function; split into array characters; remove spaces, etc. PHP language...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template