Home  >  Article  >  类库下载  >  PHP string processing built-in functions

PHP string processing built-in functions

高洛峰
高洛峰Original
2016-10-21 10:59:581292browse

PHP built-in functions:


addcslashes — Add backslash escape characters to some characters in the string
addslashes — Escape characters in the string in a specified way
bin2hex — Convert binary data into Hexadecimal representation
chop — Alias ​​function of rtrim()
chr — Returns the ASCII code of a character
chunk_split — Splits a string into small pieces according to a certain character length
convert_cyr_string — Converts Cyrillic characters to other characters
convert_uudecode — Decrypt a string
convert_uuencode — Encrypt a string
count_chars — Return character usage information in a string
crc32 — Calculate the crc32 polynomial of a string
crypt — One-way hash encryption function
echo — Used to Display some content
fprintf — Return the data as required and write it directly to the document stream
get_html_translation_table — Return an HTML entity that can be converted
hebrev — Convert a Hebrew-encoded string to visual text
hebrevc — Convert Hebrew-encoded Convert a string into visual text
html_entity_decode — The inverse function of the htmlentities () function, convert HTML entities into characters
htmlentities — Convert some characters in the string into HTML entities
htmlspecialchars_decode — The inverse function of htmlspecialchars() function, convert Convert HTML entities to characters
htmlspecialchars — Convert some characters in a string to HTML entities

explode — Convert a string into an array using delimiters

implode — Convert an array into a string using a specific delimiter
join — Convert an array into a string, alias of the implode() function
levenshtein — Calculate the difference between two words
localeconv — Get number-related format definitions
ltrim — Remove the blanks or specified characters on the left side of the string
md5_file — Encrypt a file with the MD5 algorithm
md5 — Encrypt a string with the MD5 algorithm
metaphone — Determine the pronunciation rules of a string
money_format — According to the parameters Formatted output of numbers
nl_langinfo — Query language and local information
nl2br — Replace the newline character "n" in the string with "
"
number_format — Formatted output of numbers according to parameters
ord — Replace an ASCII Convert the code into a character
parse_str — Convert a string in a certain format into a variable and value
print — Used to output a single value
printf — Display the data as required
quoted_printable_decode — Encrypt a string into an 8-bit Binary string
quotemeta — Escape several specific characters
rtrim — Remove the blanks or specified characters on the right side of the string
setlocale — Set local formats for numbers, dates, etc.
sha1_file — SHA1 a file Algorithm encryption
sha1 — Encrypt a string with SHA1 algorithm
similar_text — Compare two strings and return the number of similar characters considered by the system
soundex — Determine the pronunciation rules of a string
sprintf — Return the data as required, But it does not output
sscanf — can format strings
str_ireplace — matches and replaces strings like the str_replace() function, but is not case-sensitive
str_pad — pads both sides of the string
str_repeat — strings Perform repeated combinations
str_replace — Match and replace strings
str_rot13 — Encrypt the string with ROT13
str_shuffle — Randomly sort the characters in a string
str_split — Split a string into an array according to the character spacing
str_word_count — Obtain English word information in a string
strcasecmp — Compare strings in size, case-insensitively
strchr — Return part of a string through comparison An alias of the strstr() function
strcmp — Compare strings in size
strcoll - Compare the size of strings according to local settings
strcspn - Return the value of the continuous non-matching length of characters
strip_tags - Remove the HTML and PHP code in a string
stripcslashes - Unescap the addcslashes() function escape processing String
stripos — Find and return the position of the first match, matching is case-insensitive
stripslashes — Unescaped addslashes() function escapes the processed string
stristr — Returns parts of a string by comparison, comparison Case-insensitive
strlen — Get the encoded length of a string
strnatcasecmp — Use natural sorting to compare strings, case-insensitive
strnatcmp — Use natural sorting to compare strings
strncasecmp — Right Compare the first N characters of a string, case-insensitive
strncmp — Compare the first N characters of a string
strpbrk — Return a part of a string by comparison
strpos — Find and return the first match The position
strrchr — Returns the part of a string by comparing from back to front
strrev — Arranges all the letters in the string in reverse order
strripos — Searches from back to front and returns the position of the first match, matches are indistinguishable Uppercase and lowercase
strrpos - Search from back to front and return the position of the first match
strspn - Match and return the value of the length of consecutive occurrences of characters
strstr - Return a part of a string by comparison
strtok - Use a specified number of characters To split the string
strtolower — Convert the string to lowercase
strtoupper — Convert the string to uppercase
strtr — Compare and replace the string
substr_compare — Compare the truncated string
substr_count — Count a certain character in the string The number of occurrences of the segment
substr_replace — Replace some characters in the string
substr — Truncate the string
trim — Remove the blanks or specified characters on both sides of the string
ucfirst — Replace the first letter of the given string Convert to uppercase
ucwords — Convert the first letter of each English word in the given string to uppercase
vfprintf — Return the data as required and write it directly to the document stream
vprintf — Display the data as required
vsprintf - Return data as required, but do not output
wordwrap - Split the string according to a certain character length


Custom function:


1, the function range() to quickly create an array

For example The range() function can quickly create a number array from 1 to 9:

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:

//foreach是一种遍历数组的简便方法,foreach 仅能用于数组,当试图将其用于其它数据类型或者一个未初始化的变量时会产生错误,它有两种格式: 

foreach (array_expression as $value) statementforeach (array_expression as $key => $value) statement 
第一种格式遍历给定的 array_expression 数组。每次循环中,当前单元的值被赋给 $value 并且数组内部的指针向前移一步(因此下一次循环中将会得到下一个单元)。第二种格式做同样的事,只除了当前单元的键名也会在每次循环中被赋给变量 $key 


使用字符数组时注意大小写,比如range(A,z)和range(a,Z)是不一样的。 

range()函数还具有第三个参数,该参数的作用是设定步长,比如range(1,9,3)创建的数组元素是:1、4、7 

2,PHP中常规数组的排序 

一般数组中的各元素均以字符或数字表现的,所以可对数组元素进行升序排列,该功能函数为sort()。比如: 

升序排序后的数组元素显示为 birth name nation sex,当然,sort()函数是区分字母大小写的(字母从大到小的顺序是:A…Z…a…z) 

Sort()函数还具有第二参数,用来说明升序的规则是用来比较数字还是字符串的。比如: 

SORT_NUMERIC和SORT_STRING用来声明按数字或字符的升序排列。如果按照数字升序排列是:3,26;但如果按照字符升序排列则是:26,3了。 

PHP中除了升序函数以外,还有降序或称反向排列的函数,就是rsort()函数,比如: 

$num1=range(1,9); 
rsort($num1); //这里其实就相当于range(9,1) 

3,PHP中关联数组的排序 

PHP除了支持数字索引数组以外,还支持相关数组。比如如下数组就是一个相关(关联)数组: 

$peoples=array('xm'=>'name','xb'=>'sex','mz'=>'nation','cs'=>'birth'); 

使用sort($peoples)默认即是按照元素定义值的升序排序,在关联数组中可使用asort()函数表示按元素值升序排序,关联数组中最主要的则是可按照关键字(如xm、xb、mz等)的升序排序,该方法是用函数ksort()函数。 

'name','xb'=>'sex','mz'=>'nation','cs'=>'birth'); 
    foreach ($peoples as $mychrs) 
    echo $mychrs." "; 
    echo "
    --按元素值升序排列--
    "; 
    asort($peoples); 
    foreach ($peoples as $mychrs) 
    echo $mychrs." "; 
    echo "
    --按关键字升序排列--
    "; 
    ksort($peoples); 
    foreach ($peoples as $mychrs) 
    echo $mychrs." "; 
?>

和常规数组拥有sort()升序函数的反向排序rsort()降序函数相对应的,关联数组也有对应的降序函数:asort()函数和arsort()函数、ksort()函数和krsort()函数。 
记忆:原型函数是sort(),其中a、k表示关联数组相关必须前置,反向排序使用r修饰。 

4,PHP数组元素随机排序 

PHP中使用shuffle()函数将数组元素进行随机的重新排序,每次都会显示不同的排序组合,比如: 

5,PHP数组按原顺序反向排序 

PHP中可使用array_reverse()函数将数组元素按原顺序反向排序。比如: 


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

Related articles

See more