String Functions in PHP

Source: Internet
Author: User

1 One: Introduction to Functions2 in PHP, the operation of the string is very convenient, with some functions can solve a lot of problems. 3Demo Address: http://www.ijquery.cn/phpfunctionbase/BaseFunctionString/demo1.php4: http://Www.ijquery.cn/phpfunctionbase/BaseFunctionString/BaseFunctionString.zip5 6 Two: function code7 8<?PHP9 Header("content-type:text/html; Charset=utf-8 ");Ten Echo"//gets the string length, the number of characters, the space is also counted One $str= "Sdaf SD"; A $len=strlen($str); - Echo"One: \ $len =<font color= ' Red ' >strlen</font> ($str); The return length is: ".$len; - Echo"<br/><br/>"; the  -  - //use the string in the first argument to concatenate each element in the following array and return a string.  - $str=implode("-",Array("A", "B", "C")); + Echo"Two: \ $str =<font color= ' red ' >implode</font> (\"-\ ", Array (\" A\ ", \" B\ ", \" C\ "); The returned result is:".$str; - Echo"<br/><br/>"; +  A  at //string Segmentation method, return an array, with the characters in the first argument to split the following string, the specified character before and after the interception, if the specified character at the beginning or end of the returned array at the beginning or end of the element is an empty string//not split into a string to return to the array corresponding element a null value. The last limit returns the length of the array, which is not limited, and is always split down.  - $array=Explode("", "Hello world. It ' s a beautiful day. "); - Echo"Three: \ $array = <font color= ' red ' >explode</font> (\" \ ", \" Hello world. It ' s a beautiful day.\ "); The result returned is:<br/> "; - Print_r($array); - Echo"<br/><br/>"; -  in  - //Remove the space at the beginning of the string and return//If there is a second argument, remove the space at the beginning of the left and replace the string in the second argument. to $str=LTrim("A ASD"); + Echo"Four: \ $str =<font color= ' red ' >ltrim</font> (\" a ASD \ "); The result returned is:".$str; - Echo"<br/>"; the $str=LTrim("A ASD", "a"); * Echo"Four: \ $str =<font color= ' red ' >ltrim</font> (\" A ASD \ ", \" A\ "); The result returned is:".$str; $ Echo"<br/><br/>";Panax Notoginseng  -  the //remove whitespace from the right of the string + $str=RTrim("ASD"); A Echo"V: \ $str =<font color= ' red ' >rtrim</font> (\" ASD \ "); The result returned is:".$str; the Echo"<br/><br/>"; +  - //rejects the string that begins with the second argument on both sides of the first string. If there is no second argument, the default is to remove the space between the beginning of the string $ $str=Trim("Sdsdfas"); $ Echo"Six: \ $str =<font color= ' red ' >trim</font> (\" Sdsdfas \ "); The result returned is:".$str; - Echo"<br/>"; - $str=Trim("A Sdsdfas a", "a"); the Echo"Six: \ $str =<font color= ' red ' >trim</font> (\" A Sdsdfas a\ ", \" A\ "); The returned result is:".$str; - Echo"<br/><br/>";Wuyi  the  - //How long (how many) characters are taken from the specified position in the first argument of the string, and the first character position in the string is calculated from 0. If the second argument is negative, the string is taken from the end of the string to the beginning of the last number. The last character at the end counts-1, and the intercept direction is always left to right. Wu $str=substr("Abcdefgh", 0,4); - Echo"Seven: \ $str =<font color= ' red ' >substr</font> (\" Abcdefgh\ ", 0,4); The result returned is:".$str; About Echo"<br/><br/>"; $  -  - //Replace the first argument string of the third argument with the parameter two string - $str=Str_replace("A", "", "ABCABCABCA")); A Echo"Eight: \ $str =<font color= ' red ' >str_replace</font> (\" A\ ", \" \ ", \" Abcabcabca\ "); The returned result is:".$str; + Echo"<br/><br/>";//Same as Str_replace usage, only case insensitive the $str=Str_ireplace("A", "", "ABCABCABCA")); - Echo"Nine: \ $str =<font color= ' red ' >str_ireplace</font> (\" A\ ", \" \ ", \" Abcabcabca\ "); The returned result is:".$str; $ Echo"<br/><br/>"; the  the  the //string that returns the character of the string in parentheses in all uppercase letters the $str=Strtoupper("Sdaf"); - Echo"Ten: \ $str =<font color= ' red ' >strtoupper</font> (\" sdaf\ "); The result returned is:".$str; in Echo"<br/><br/>"; the  the  About //returns the first string in parentheses after it becomes uppercase the $str=Ucfirst("Asdf"); the Echo"11: \ $str =<font color= ' red ' >ucfirst</font> (\" asdf\ "); The result returned is:".$str; the Echo"<br/><br/>"; +  - //Use Echo to print the bracketed string in parentheses, including the label signature the $str=htmlentities("<br/>");Bayi Echo"12: \ $str =<font color= ' red ' >htmlentities</font> (\" < b r >\ ");(the above BR no spaces) The result returned is:".$str; the Echo"<br/><br/>"; the  - //returns the number of occurrences of the second argument string in the first string - $int=Substr_count("Abcdeabcdeablkabd", "AB"); the Echo"13: \ $int =<font color= ' red ' >substr_count</font> (\" abcdeabcdeablkabd\ ", \" Ab\ "); The returned result is:".$int; the Echo"<br/><br/>"; the  the  - //returns the position of the second string at the first occurrence of the first string, the first character position 0 the $int=Strpos("Asagaab", "AB"); the Echo"14: \ $int =<font color= ' red ' >strpos</font> (\" Asagaab\ ", \" Ab\ "); The returned result is:".$int; the Echo"<br/><br/>";94  the  the  the //returns the position of the second string at the last occurrence of the first string, the first character position 098 $int=Strrpos("Asagaabadfab", "AB"); About Echo"15: \ $int =<font color= ' red ' >strrpos</font> (\" Asagaabadfab\ ", \" Ab\ "); The returned result is:".$int; - Echo"<br/><br/>";101 102 103 //intercepts a string that returns the first argument from left to right in the return parameter one to the last character of the argument104 $str=strstr("Sdafabsdgaabdsfgs", "AB"); the Echo"16: \ $str =<font color= ' red ' >strstr</font> (\" Sdafabsdgaabdsfgs\ ", \" Ab\ "); The returned result is:".$str;106 Echo"<br/><br/>";107 108 109 //intercepts a string that returns the last argument from left to right in the return parameter one to the last character of the argument the $str=STRRCHR("Sdafsdgaababdsfgs", "AB");111 Echo"17: \ $str =<font color= ' red ' >strrchr</font> (\" Sdafsdgaababdsfgs\ ", \" Ab\ "); The returned result is:".$str; the Echo"<br/><br/>";113  the //Add "\" to each character in parameter two before the same character in parameter one the $str=addcslashes("Abcdefghijklmn", "AKD"); the Echo"18: \ $str =<font color= ' red ' >addcslashes</font> (\" abcdefghijklmn\ ", \" akd\ "); The returned result is:".$str;117 Echo"<br/><br/>";118 119 //fills the string of parameter one to the length specified in parameter two (number of single characters), the parameter three is the specified padding string, does not write the default space//parameter four fill position, 0 on the left side of the parameter fill, 1 to the right, and 22 sides at the same time. Do not write default at the right start padding - $str=Str_pad("Abcdefgh1", Ten, "at", 0);121 Echo"19: \ $str =<font color= ' red ' >str_pad</font> (\" abcdefgh1\ ", 10,\" at\ ", 0); The returned result is:".$str;122 Echo"<br/><br/>";123 124 //compare the two strings corresponding to the character of the Andean Code value, the first pair is not the same, if the parameter one is greater than the return 1 in the parameter two, and vice versa returns-1, the two strings return exactly the same 0 the $int=strcmp("B", "a");126 Echo"20: \ $int 1=<font color= ' red ' >strcmp</font> (\" B\ ", \" A\ "); The returned result is:".$int;127 Echo"<br/><br/>"; - 129 //return the first parameter after formatting the number format, the second parameter is to retain a few decimals, the parameter three is to change the decimal point to the parameter three, the parameter four is the integer part every three bits with what character division//After three parameters are not written, the default minus the fractional part, integer every three bits with commas, split. Parameter three, parameter four must exist simultaneously the $str=Number_format(1231233.1415,2, "D", "a");131 Echo"21: \ $str =<font color= ' Red ' >number_format</font> (1231233.1415,2,\" d\ ", \" A\ "); The returned result is:".$str; the Echo"<br/>";133 $str=Number_format(1231233.1415,2);134 Echo"21: \ $str =<font color= ' Red ' >number_format</font> (1231233.1415,2); The result returned is:".$str;

String Functions in PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.