Home  >  Article  >  Backend Development  >  Commonly used string processing functions in php

Commonly used string processing functions in php

WBOY
WBOYOriginal
2016-07-25 08:45:13806browse
1Find character position function:
strpos($str,$seach,[int]); Find the first position of search in $str starting from int;
stripos($str,$seach,[int]); The function returns the position of the first occurrence of a string in another string. This function is not case sensitive
strrpos($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. Function to extract substrings
substr($str,int start,[int len])
strstr($str1,str2)
stristr() has the same function as strstr, except that it is not case sensitive
strrchr() returns from the last character searched; use: to get the file name in the path


3. PHP string function to replace string
str_replace(search,replace,source) finds search and replaces it in source
str_ireplace(search,replace,$str): Same as above, this function is not case sensitive
strtr ($str, $from, $to) This function returns a copy of str and converts the characters specified in from to the corresponding characters in to
$trans = array("hello" => "hi", "hi" => "hello");
echo strtr("hi all, I said hello", $trans);
?>
substr_replace($Str,$rep,$start[,length]): $str original string, $rep new string after replacement, $start starting position, $length replacement length, this item is optional


4. Character length
int strlen($str)


5. Compare character function
int strcmp($str1,$str2)$str1>= strcasecmp() ignores case (same as above)
strnatcmp("8","18") compares strings in natural order
strnatcasecmp()


6. PHP string function split into arrays
str_split($str,len)
split(search,$str,[int])
explode(search,$str,[int]) splits $str according to the search characters and returns the array int. How many times will it be split? The rest will not be split.


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() converts n into

strip_tags($str,['

']) removes HTML and PHP tags
Such as: echo strip_tags($text, '

')
htmlspecialchars($str[,var])


11. PHP string function for character case conversion
strolower(str)
stroupper(str)
ucfirst(str) converts the first character of the function to uppercase
ucwords(str) converts the first letter of each word to uppercase


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

php


Statement:
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