1. Calculate the byte length function strlen ($STR)
$str= ' Hello world! '; // Calculate byte length function strlen ($STR) Echo ' ' ',$str, ' The length is ',strlen($str), ' <br> ';
Output: "Hello world!" The length is 12
2. Calculate the character length function mb_strlen ($str, ' encoding (e.g. Utf-8) ')
$str 1= ' I am Chinese '; // calculates the character length function Mb_strlen ($str, ' encoding (e.g. Utf-8) ') Echo ' ' ',$str 1, ' "', ' the number of bytes is ',strlen($str 1), ', the number of characters is ', Mb_strlen ($str 1, ' utf-8 '), ' < Br> ';
Output: "I am Chinese" the number of bytes is 15, the number of characters is 5
3. Determine the position of the substring in the string strpos ($str 1, $str), where $STR1 is a substring
$str 2= ' Hello world ';//determines the position of the substring in the string Strpos ($str 1, $str), where $STR1 is a substring$s= ' World ';if(($a=Strpos($str 2,$s))!==false){ Echo‘"‘,$s, ' ' ', ' in ',$str 2, ' The first of the ',$a, ' a position ';}Else{ Echo‘"‘,$s, ' ' ', ' does not exist ',$str 2, ' in ';}Echo' <br> ';
Output: "World" in the 6th position in "Hello World"
4. String substitution function str_replace ($str 1, $str 2, $str), where $str1 is the replaced string, $str 2 is the replaced string
$str 3= ' it\ ' s bullshit '; // The string substitution function Str_replace ($str 1, $str 2, $str), where $str1 is the substituted string, $str 2 is the replaced string $str 4=str_replace(' bullshit ', ' * * ',$str 3); Echo ' "',$str 3, '" after substitution becomes "',$str 4, '" <br> ";
Output: "It's bullshit" replaced and became "it's * * *"
5. Replace a large number of String Strtr ($str, Array ())
$str 5= ' Some people live as if they were dead, some die but still live '; // Replace a large number of strings STRTR ($STR, Array ()) $str 6=strtr($str 5,array// replace Live with dead, die replace alive echo ' "', $str 5, ' "replaced by" ',$str 6, ' "<br>";
Output: "Some people live as if they were dead, and some die and still live" replaced by "some people die as if they live, some live and still die."
6.substr ($str, $a, $b) intercepts the string, $a represents the position at which the Intercept begins, $b represents the Intercept length
$STR 7= ' My Pleasure,sir ';//substr ($str, $a, $b) intercepts the string, $a represents the position at which the Intercept begins, $b represents the Intercept lengthEcho substr($STR 7, 3,8), ' <br> '; Echo substr($STR 7, 2,8), ' <br> '; Echo substr($STR 7, 3), ' <br> ';//$b do not write, the default intercept to the lastEcho substr($STR 7, 3,-2), ' <br> ';//$b is negative, it will be truncated to the B-th characterEcho substr($STR 7, -3), ' <br> '; Echo substr($STR 7, -3,2), ' <br> ';//The $a is a negative number, and a is a representation of the inverse of a string positionEcho substr($STR 7, -3,-2), ' <br> ';//$a, $b are negative, both represent positions
Output Result:
Pleasure
Pleasur
Pleasure,sir
Pleasure,s
Sir
Si
S
7. The string splitter function explode ($s, $str) $s is a split node, and the result returns an array
$str 8= ' Hello, I am Chinese '; // The string splitter function explode ($s, $str) $s is a split node, and the result returns an array Print_r (explode(', ',$str 8));
Output: Array ([0] = = Hello [1] = = I am Chinese)
8. The array is converted to a string function implode ($s, $arr)$arr is an array, $s is the associative node
$arr=array//implode ($s, $arr) $arr is an array, $s is the associative node echo Implode(', ',$arr);
Output Result: Hello,world
PHP Learning Notes-string function exercises