This article mainly introduces the commonly used string functions in PHP, the interested friend's reference, I hope to help you.
Read Catalogue
Determine string length
Comparing strings
Split connection reversal
HTML and strings are converted to each other
Populating and rejecting strings
Count the number of characters and words
Find Alternate Intercept
Casing Processing
PHP contains 98 string functions (in addition to functions based on regular expressions, regular expressions are not discussed here), can handle every aspect of the string can be encountered, this article on the common string functions of a simple summary, mainly consists of the following 8 parts: 1. Determine string length, 2. Compare strings, 3. Split connection inversion, 4.html and string conversion, 5. Fill and reject strings, 6. Count characters and number of words, 7. Find replacement Intercept, 8. Case processing.
Back to Catalog
Determine string length
The Strlen function and the Mb_strlen function, which need to open the mbstring extension
<?php Header (' Content-type:text/html;charset=utf-8 '); $str = ' abcdef '; echo strlen ($STR); 6 echo "<br/>"; $STR = ' ab CD '; echo Mb_strlen ($STR); 7 echo "<br/>"; Strlen is the count of the string "byte" Length //mb_strlen, which is based on the encoding that computes the number of characters in the string. $str = ' People's Republic of China '; echo "byte length is". strlen ($STR);//In UTF-8 encoding, a Chinese character accounts for 3 bytes in GBK a Chinese character occupies 2 bytes echo "<br/>"; echo "character length is". Mb_strlen ($str, ' utf-8 ');?>
Back to Catalog
Comparing strings
strcmp function, strcasecmp function, strspn function, strcspn function
<?php $pwd = "Userpwd"; $pwd 2= "Userpwd"; Case-sensitive if (strcmp ($pwd, $pwd 2)!=0) { echo "password does not match"; } else{ echo "password Match"; } $email 1= "www.baidu.com"; $email 2= "WWW.BAIDU.COM"; Case insensitive if (!strcasecmp ($email 1, $email 2)) { echo "OK", ' <br> '; } Ask for the same part of the two string $password = "1233345"; if (STRSPN ($password, "1234567890") ==strlen ($password)) { echo "The password connot consist solely of numbers"; } // $password = "a12345"; if (STRCSPN ($password, "1234567890") ==0) { echo "The password connot consist solely of numbers"; } ? >
Back to Catalog
Split connection reversal
Str_split functions, split functions, explode functions, and implode functions
<?php header (' Content-type:text/html;charset=utf-8 '); $str = "Hello Friend"; $arr 1 = str_split ($STR); Print_r ($arr 1); $arr 2 = Str_split ($STR, 3); Print_r ($arr 2); $STR = ' abc, China, USA, Japan '; Explode, the string is divided into groups according to the specified delimiter. $arr = Explode (', ', $str); Print_r ($arr); Implode, the array is then stitched into a string according to the specified connector $arr = Explode (', ', $str); echo implode (' ~ ', $arr), ' <br/> '; You can only pass an array to do the arguments, do not specify the connector, //so that the array unit will be directly spliced together Echo implode ($arr);?>
Back to Catalog
HTML and strings are converted to each other
Htmlspecialchars functions, Strip_tags functions, get_html_translation_table functions and addcslashes functions and Htmlentities functions
<?php $str = "Hello", "World"; echo $str, ' <br/> '; echo $str = Addslashes ($str), ' <br/> '; echo stripslashes ($str), ' <br/> '; $str = ' <ab> '; echo $str, ' <br/> '; echo Htmlspecialchars ($STR); echo "</br>"; $str = "Email <a href= ' admin@qq.com ' >example@qq.com</a>"; echo Strip_tags ($STR);?>
Back to Catalog
Populating and rejecting strings
Trim function, LTrim function, RTrim function, Str_pad function, Chunk_split function
<?php $str = ' 12345678 '; Echo Chunk_split ($STR, 3, ', '); echo "<br>"; $text = "\t\tthese is a few words:) ... "; echo Trim ($text); echo "<br>"; Echo LTrim ($text, ' \ t '), ' <br> '; Echo RTrim ($text, ' \ R '), ' <br> '; echo str_pad (' Apple ', 6). " is good. ";? >
Back to Catalog
Count the number of characters and words
Count_chars functions and Str_word_count
<?php $data = "Both Ts and one F."; foreach (Count_chars ($data, 1) as $i = + $val) { echo "There were $val instance (s) of" ", Chr ($i)," \ "in the Str Ing.\n "; } echo "
Back to Catalog
Find Alternate Intercept
Strpos function, str_replace function, substr_replace function, substr function, Strstr function
<?php $substr = "index.html"; $log = <<< logfile 192.168.1.11:/www/htdocs/index.html:[2016/08/10:21:58:27] 192.168.1.11:/www/ HTDOCS/INDEX.HTML:[2016/08/18:01:51:37] 192.168.1.11:/www/htdocs/index.html:[2016/08/20:11:48:27]logfile; $pos =strpos ($log, $substr); $pos 2=strpos ($log, "\ n", $pos); $pos = $pos +strlen ($substr) +1; $timestamp =substr ($log, $pos, $pos 2-$pos); echo "The file $substr was first accessed on: $timestamp"; echo "<br>"; $author = "lester@example.com"; $author =str_replace ("@", "at", $author); echo "Connect the author of this article at $author"; echo "<br>"; Echo LTrim (Strstr ($author, "@"), "@");?>
Back to Catalog
Casing Processing
Strtolower function, Strtoupper function, Ucfirst function, Ucwords function
<?php $url = "Http://WWWW.BAIDU.COM"; echo Strtolower ($url), ' <br> '; $str = "Hello World"; echo Strtoupper ($str), ' <br> '; $str = "PHP is the most popular language"; echo Ucfirst ($str), ' <br> '; echo Ucwords ($STR);?>