В нашей повседневной работе нам часто приходится иметь дело с некоторыми строками или массивами. Сегодня у нас есть время разобраться в них
Строковая операция
php //String truncation $str = 'Hello World!' Substr ($STR, 0,5); // return 'hello' //Chinese string truncation $STR = 'Hello, Shenzhen'; $result = mb_ Substr ($STR, 0,2); // Hello //First occurrence of lookup string $emai = "[email protected]"; $domain = strstr ($email, '@'); // return '@ 163. Com' $domain = strstr ($email,'@ ','true'); // return '123'. //Finds the first occurrence of a string in another string. $emai = "[email protected]"; Strpos ($Emai, '@'); // return '3' //Break a string into arrays $STR = 'Hello, Shenzhen'; $arr = expand (', $STR); // the return value is array (2) {[0] = > string (6) "hello" [1] = > string (6) "Shenzhen" //String length $str = "Hello world"; $STR1 = strlen ($STR); // return 11 //Converts the first character in a string to uppercase. $str = "hello world"; $STR1 = ucfirst ($STR); // return "Hello world" //Converts the first character in a string to lowercase. $str = "Hello world"; $STR1 = lcfirst ($STR); // return "Hello world" //Converts the first character of each word in a string to uppercase. $str = "hello world"; $STR1 = ucwords ($STR); // return "Hello world" //Reverse string $str = "hello world"; $STR1 = strrev ($STR); // return "dlrow olleh" //Replace some characters in a string $str = "hello world"; $str1= str_ Replace ('World ',' Lisa ', $STR); // return "Hello Lisa" //String to uppercase $str = "hello world"; $STR1 = strtoupper ($STR); // return "Hello world" //String to lowercase $str = "HELLO WORLD"; $STR1 = strtolower ($STR); // return "Hello world" ?>
Работа с массивом
//Массив в строку
$arr = ['aa','bb','cc']; $STR = implode (',, $ARR); // output "AA, BB, CC" //Key value of array $arr = ['aa','bb','cc']; $aa = array_ Keys ($ARR); // output array (3) {[0] = > int (0) [1] = > int (1) [2] = > int (2)} //Merge arrays $a1=array("a"=>"red","b"=>"green"); $a2=array("c"=>"blue","b"=>"yellow"); print_ r(array_ Merge ($A1, $A2)); // returns array (4) {["a"] = > string (5) "test1" [0] = > string (2) "BB" [1] = > string (2) "CC" [2] = > string (5) "test2"}. Note that when two array keys are the same, the last one will overwrite other elements