Home >Backend Development >PHP Tutorial >PHP string function collection_PHP tutorial
1 Find character position function:
strpos($str,search,[int]): Find the first position of search in $str starting from int;
stripos($str,search,[int]) :Function returns the position of the first occurrence of a string within another string. This function is not case-sensitive
strrpos($str,search,[int]): Find the last occurrence of search in $str from int
2. Extract sub-character function (double Bytes)
submit($str,int start[,int length]): Extract [length-length string] starting from the strat position in $str.
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() has the same function as strstr, except that it is not case-sensitive.
strrchr() returns from the last searched character; use: get the file name in the path
3. Replace string
str_replace(search,replace,$str): from $str Use replace to search in search to replace
str_irreplace(search,replace,$str):
strtr($str,search,replace): replace in this function cannot be "";
substr_replace($Str,$rep,$start[,length])$str original string, $rep replaced new
string, $start starting position, $length replaced Length, 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 (not case sensitive)
strnatcmp("4", "14") Compare strings in natural order
strnatcasecmp() Same as above, (case sensitive)
6. Split into array function
str_split($str,len): Split $str according to the length of len Return array
split(search,$str[,int]): Split $str according to search characters. Return array int how many times it is split, and 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. Add a space;
9. chr, ord--return the specified character or ascii
10. HTML code-related functions
nl2br(): convert n to < ;br>.
strip_tags($str[,'
']): Remove HTML and PHP tags
All HTML and PHP code in $str will be removed, optional parameters are html and PHP code The function is to retain the code written by the
optional parameters.
For example: echo strip_tags($text, '
');
htmlspecialchars($str[,parameter]): The normal output of the page HTML code parameters is conversion Method
11. Character case conversion function
strtolower($str) Convert string to lowercase
strtoupper($str) Convert string to uppercase
ucfirst($str) Function Convert the first character of each word to uppercase
ucwords($str) Convert the first letter of each word to uppercase
12. Database related function
addslashes($str): Make str single Quotes ('), double quotes ("), backslash () and NUL
strings are converted to ',",\.
magic_quotes_gpc = On 自动对 get post cookie的内容进行转义
get_magic_quotes_gpc()检测是否打开magic_quotes_gpc
stripslashes() 去除字符串中的反斜杠
13、连接函数
implode(str,$arr) 将字符串数组按指定字符连接成一个字符串;implode()函数有个别名函数join
addcslashes —— 为字符串里面的部分字符添加反斜线转义字符
addslashes —— 用指定的方式对字符串里面的字符进行转义
bin2hex —— 将二进制数据转换成十六进制表示
chr —— 返回一个字符的ASCII码
chunk_split —— 按一定的字符长度将字符串分割成小块
convert_cyr_string —— 将斯拉夫语字符转换为别的字符
convert_uudecode —— 解密一个字符串
convert_uuencode —— 加密一个字符串
count_chars —— 返回一个字符串里面的字符使用信息
crc32 —— 计算一个字符串的crc32多项式
crypt —— 单向散列加密函数
explode —— 将一个字符串用分割符转变为一数组形式
fprintf —— 按照要求对数据进行返回,并直接写入文档流
get_html_translation_table —— 返回可以转换的HTML实体
html_entity_decode —— htmlentities ()函数的反函数,将HTML实体转换为字符
htmlentities —— 将字符串中一些字符转换为HTML实体
htmlspecialchars_decode —— htmlspecialchars()函数的反函数,将HTML实体转换为字符
htmlspecialchars —— 将字符串中一些字符转换为HTML实体
implode —— 将数组用特定的分割符转变为字符串
join —— 将数组转变为字符串,implode()函数的别名
levenshtein —— 计算两个词的差别大小
localeconv —— 获取数字相关的格式定义
ltrim —— 去除字符串左侧的空白或者指定的字符
md5_file —— 将一个文件进行MD5算法加密
md5 —— 将一个字符串进行MD5算法加密
metaphone —— 判断一个字符串的发音规则
money_format —— 按照参数对数字进行格式化的输出
nl_langinfo —— 查询语言和本地信息
nl2br —— 将字符串中的换行符“\n”替换成“
”
number_format —— 按照参数对数字进行格式化的输出
ord —— 将一个ASCII码转换为一个字符
parse_str —— 把一定格式的字符串转变为变量和值
print —— 用以输出一个单独的值
printf —— 按照要求对数据进行显示
quoted_printable_decode —— 将一个字符串加密为一个8位的二进制字符串
quotemeta —— 对若干个特定字符进行转义
rtrim —— 去除字符串右侧的空白或者指定的字符
setlocale —— 设置关于数字,日期等等的本地格式
sha1_file —— 将一个文件进行SHA1算法加密
sha1 —— 将一个字符串进行SHA1算法加密
similar_text —— 比较两个字符串,返回系统认为的相似字符个数
soundex —— 判断一个字符串的发音规则
sprintf —— 按照要求对数据进行返回,但是不输出
sscanf —— 可以对字符串进行格式化
str_ireplace —— 像str_replace()函数一样匹配和替换字符串,但是不区分大小写
str_pad —— 对字符串进行两侧的补白
str_repeat —— 对字符串进行重复组合
str_replace —— 匹配和替换字符串
str_rot13 —— 将字符串进行ROT13加密处理
str_shuffle —— 对一个字符串里面的字符进行随机排序
str_split —— 将一个字符串按照字符间距分割为一个数组
str_word_count —— 获取字符串里面的英文单词信息
strcasecmp —— 对字符串进行大小比较,不区分大小写
strchr —— 通过比较返回一个字符串的部分strstr()函数的别名
strcmp —— 对字符串进行大小比较
strcoll – 根据本地设置对字符串进行大小比较
strcspn —— 返回字符连续非匹配长度的值
strip_tags —— 去除一个字符串里面的HTML和PHP代码
stripcslashes —— 反转义addcslashes()函数转义处理过的字符串
stripos —— 查找并返回首个匹配项的位置,匹配不区分大小写
stripslashes —— 反转义addslashes()函数转义处理过的字符串
stristr —— 通过比较返回一个字符串的部分,比较时不区分大小写
strlen —— 获取一个字符串的编码长度
strnatcasecmp —— 使用自然排序法对字符串进行大小比较,不区分大小写
strnatcmp —— 使用自然排序法对字符串进行大小比较
strncasecmp —— 对字符串的前N个字符进行大小比较,不区分大小写
strncmp —— 对字符串的前N个字符进行大小比较
strpbrk - Returns a part of a string by comparing
strpos - Finds and returns the position of the first match
strrchr - Returns a part of a string by comparing from back to front
strrev - Arrange all letters in the string in reverse order
strripos - search from back to front and return the position of the first match, matching is not case sensitive
strrpos - search from back to front and return the first one The position of the matching item
strspn - Match and return the value of the length of consecutive occurrences of characters
strstr - Return the part of a string by comparison
strtok - Split the string with the specified number of characters
strtolower - Convert the string to lowercase
strtoupper - Convert the string to uppercase
strtr - Compare and replace the string
substr_compare - Compare the string after interception
substr_count - Count the number of occurrences of a certain character segment in a string
substr_replace - Replace some characters in the string
substr - Truncate the string
trim - Remove both sides of the string Blanks or specified characters
ucfirst - Convert the first letter of the given string to uppercase
ucwords - Convert the first letter of each English word in the given string to uppercase
vfprintf - Returns data as required and writes directly into the document stream
vprintf - Displays data as required
vsprintf - Returns data as required, but does not output
wordwrap —— Split the string according to a certain character length
1, and quickly create the array function range()
For example, the range() function can quickly create an array from 1 to 9 Number array:
$numbers=range(1,9); //Use range to directly create an array of 9 numbers from 1 to 9, starting with "1" 9" ends.
echo $numbers[1]; //Output the second created array value: 2; echo $numbers[0]; then enter the first value: 0.
?>
Of course, using range(9,1) creates a number array from 9 to 1. At the same time, range() can also create a character array from a to z:
$numbers=range(a,z);
foreach ($numbers as $mychrs) //Traverse the $numbers array, and the current cell value is assigned to $mychrs each time it loops
echo $mychrs." "; //output a b c d e f g h i j k l m n o p q r s t u v w x y z
?>
//foreach is a A convenient way to iterate over an array. foreach can only be used for arrays. An error will occur when trying to use it for other data types or an uninitialized variable. It has two formats:
foreach (array_expression as $value) statementforeach (array_expression as $key => $value) statement
The first format traverses the given array_expression array. Each time through the loop, the value of the current cell is assigned to $value and the pointer inside the array is moved forward one step (so the next cell will be obtained in the next loop). The second format does the same thing, except that the key name of the current unit will also be assigned to the variable $key in each loop
Pay attention to the case when using character arrays, such as range(A ,z) and range(a,Z) are different.
The range() function also has a third parameter, which is used to set the step size. For example, the array elements created by range(1,9,3) are: 1, 4, 7
2. Sorting of conventional arrays in PHP
Generally, each element in an array is represented by characters or numbers, so the array elements can be arranged in ascending order. This function is sort(). For example:
$people=array('name','sex','nation','birth');
foreach ($people as $mychrs)
echo $mychrs." ";
sort($people);
echo "
---After sorting---
";
foreach ($ people as $mychrs)
echo $mychrs." ";
?>
The array elements sorted in ascending order are displayed as birth name nation sex. Of course, the sort() function distinguishes letter sizes Written (the order of letters from large to small is: A...Z...a...z)
The Sort() function also has a second parameter, which is used to indicate whether the ascending order rule is used to compare numbers or strings of. For example:
echo "---Sort in ascending numerical order---
";
$num2=array('26','3' ,);
sort($num2,SORT_NUMERIC);
foreach ($num2 as $mychrs)
echo $mychrs." ";
echo "
- --Sort in ascending character order---
";
$num3=array('26','3');
sort($num3,SORT_STRING);
foreach ( $num3 as $mychrs)
echo $mychrs." ";
?>
SORT_NUMERIC and SORT_STRING are used to declare ascending order of numbers or characters. If arranged in ascending order of numbers, it is: 3, 26; but if arranged in ascending order of characters, it is: 26, 3.
In addition to the ascending function, PHP also has a descending or reverse sorting function, which is the rsort() function, for example:
$num1=range(1,9);
rsort($num1); //This is actually equivalent to range(9,1)
3, sorting of associative arrays in PHP
In addition to supporting numerical index arrays, PHP also supports Related arrays.For example, the following array is a related (associative) array:
$peoples=array('xm'=>'name','xb'=>'sex','mz'=>'nation ','cs'=>'birth');
Using sort($peoples) defaults to sorting in ascending order of element definition values. In an associative array, the asort() function can be used to indicate by element value Sorting in ascending order. The most important thing in associative arrays is to sort in ascending order of keywords (such as xm, xb, mz, etc.). This method is to use the function ksort() function.
$peoples=array('xm'=>'name','xb'=>'sex','mz'=>'nation','cs '=>'birth');
foreach ($peoples as $mychrs)
echo $mychrs." ";
echo "
--In ascending order by element value Sort--
";
asort($peoples);
foreach ($peoples as $mychrs)
echo $mychrs." ";
echo "< ;br />--Sort in ascending order by keyword--
";
ksort($peoples);
foreach ($peoples as $mychrs)
echo $mychrs." ";
?>
Corresponding to the reverse sorting rsort() descending function that has the sort() ascending function for regular arrays, associative arrays also have corresponding descending functions: asort() function and arsort() function , ksort() function and krsort() function.
Memory: The prototype function is sort(), where a and k represent associative arrays that must be preceded, and reverse sorting is modified with r.
4, PHP array elements are randomly sorted
The shuffle() function is used in PHP to randomly reorder the array elements, and different sorting combinations will be displayed each time, such as:
$fer=array('cnbruce','cnrose','cnjames','cnanne');
shuffle($fer); //Sort immediately, each time the page is refreshed Perform a random sorting.
foreach ($fer as $mychrs)
echo $mychrs." ";
?>
5, PHP array is sorted in reverse order
You can use the array_reverse() function in PHP to sort the array elements in reverse order. For example:
$fer=array('cnbruce','cnrose','cnjames','cnanne');
foreach ($fer as $mychrs)
echo $mychrs." ";
$fer=array_reverse($fer); //Reverse sort the elements in the array in the original order
echo "
--In the original order Reverse order--
";
foreach ($fer as $mychrs)
echo $mychrs." ";
?>
output: cnbruce cnrose cnjames cnanne
--reverse in original order--
cnanne cnjames cnrose cnbruce
Note that $fer=array_reverse($fer); any sorting function before this is just a statement reference , the original array is not redefined, but it needs to be redefined when I debug the function. This is because array_reverse() returns a modified copy of the original array. If you do not need the original array, you can redefine the original array to overwrite it. Otherwise, define another array to save the copy, for example: $fer_bak=array_reverse($fer_bak );