Home >Backend Development >PHP Tutorial >Commonly used string processing functions in php
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
')
|