/**
- * PHP string function example
- * By bbs.it-home.org
- */
$ Str1 = "aBCD ";
- Print ($ str1); // output function, with returned results (strings). The speed is not as fast as echo!
// Echo is not a PHP function.
- Print_r ($ str); // output function, which is generally used for testing
- Echo"
**** ". Ord ($ str1 )." ";
- $ Str2 = "aBCD ";
- Echo"
**** ". Ord ($ str2 )." ";
Echo strcmp ($ str2, $ str1); // whether the ASCII value of the first letter a of str1 is greater than the ASCII value of the first letter of str2
- // 1 is returned, not-1. the same ASCII code value continues to be compared! Returns 0 if they are identical.
- Echo"
";
Echo substr_count ("abcd bcxx", "bc"); // counts the number of times a string bc appears before the preceding character. no 0 is returned.
- // Echo substr_count ("abcd bcxx", "bc", 3, 4); // counts the string bc from the previous string, starting from 3rd characters to the next 4 characters // Number of times bc appears, no 0 is returned
- // Echo substr_count ("abcd bcxx", "bc",); // An error is returned if the length is exceeded.
Echo" Strpos and strrpos functions ";
- Echo strpos ("abcde", "bc"); // the position where the bc string first appears in the abcde string. If no position is displayed, null is returned.
- Echo strrpos ("abcde", "bc1"); // indicates the position of the bc string in the last occurrence of the abcde string. If no result is displayed, null is returned.
Echo" Strstr and strrchr functions ";
- // Echo strstr ("abced", "bc"); // the output string bc starts from the position where the first occurrence of the abced string to the following string. no null is returned.
- Echo"
";
- Echo strrchr ("bcabced", "1"); // the output string bc1 starts at the position where the last occurrence of the abced string to the next string without taking the first character, search for the preceding string and find it to return it. if it is still
- If no value is found, null is returned.
- Echo strrchr ("bcabced China", "medium ");
Echo" Nl2br function ";
- $ Str = "afdgdsa \ r \ n1111 ";
- Echo nl2br ($ str); // converts escape carriage return and line feed to html
Echo" Str_repalce function ";
- Echo $ str = "dsfsd sdfsdf 233 ";
- Echo "\ t original string length:". strlen ($ str )."
";
- Echo strlen (str_replace ("", "", $ str); // replace spaces in the str string with strings that do not exist. for example, the string to be searched has no characters to replace, no operation will be performed !!
- $ Str = "[dsfsdf] sdfsdf [sdfsdf]";
- $ Arr1 = array ("{","}");
- $ Arr2 = array ("(");
- $ Str = str_replace ($ arr1, $ arr2, $ str );
- Echo $ str;
Echo" Substr function ";
- Echo substr ("abcddsfds", 2 )."
"; // The characters starting from the second position to the end of the string
- Echo substr ("abcddsfds", 2nd); // truncates 20 characters from the first position to the string
Echo" Explode and str_split functions ";
- $ Str = "1, 2, 3, 4, 5 ,";
- Print_r (explode ("(", $ str); // to split a string without any characters. a string is directly returned.
- Foreach (explode (",", $ str) as $ v ){
- Echo $ v. "\ t ";
- }
- $ Str = "1, 2, 3, 4, 5 ,";
- Echo"
";
- Print_r (explode ("2", $ str, 4 ));
- Echo"
";
- $ Str = "script School 22222 ";
- Print_r (str_split ($ str, 2); // splits the string by two bytes. Chinese characters are not supported.
- ?>
-
|