PHP string functions and array functions

Source: Internet
Author: User
Tags rtrim server array string format

/Verification Code
$str = "abcdefghijklmnopqrstuvwxyz0123456789";
$a =substr ($str, Rand (0,35), 1);
$b =substr ($str, Rand (0,35), 1);
$c =substr ($str, Rand (0,35), 1);
$d =substr ($str, Rand (0,35), 1);
echo $a. $b. $c. $d;

The length of the output string
$ABC = "Have you eaten?";
echo strlen ($ABC);


Splitting a string, dividing the array into groups
$ABC = "Gently I come, as I quietly walk, waving a wave of sleeves, not take away a cloud";
$arr =explode (",", $ABC);
$count =count ($arr); Look at the value after the split
Echo $count;
Print_r ($arr);


String connection
$arr =array (0=> "My Car",1=> "is the Red",2=> "BMW");
$abc =implode (",", $arr); Connect with commas in the middle
Echo $abc;


Uppercase and lowercase letters conversion
$str = "Adasdfjs";
$str =strtoupper ($STR); Convert all lowercase letters to uppercase
$str =ucfirst ($STR);//Convert first letter to uppercase
$str =strtolower ($STR); Convert all uppercase letters to lowercase letters
$str =ucfirst (Strtolower ($STR)); Converted to only the first letter is uppercase
Echo $str;


Convert HTML format to string format
$str = "<table width=200 border=1><tr><td> why people live </td></tr></table>";
$str =htmlspecialchars ($STR);
Header ("content-type:text/html;charset=gb2312"); Choose an encoding format
Echo $str;


Remove spaces
$ABC = "One must depend on oneself";
echo "People". RTrim (LTrim ($ABC)). " //rtrim is to remove the right space LTrim is to remove the left space, trim is to remove both sides of the space


View package contains no characters
$str = "I saw someone hanging a sheep's head in the street selling bombs";
$AA = "Bomb";
$num =substr_count ($str, $AA);
if ($num >0) {
echo "character contains illegal keywords";
}


See where a character appears
$STR = "I see the street, someone hangs a sheep's head in the street to sell bombs";
$AA = "Main Street";
$wz =strpos ($str, $AA); This is where the First Avenue appears.
$wz =strrpos ($str, $AA); This is where the last street came from.
Echo $wz;


Add removal \
$STR = "I see the street, someone in the street ' vinegar ' sell bombs";
$str =addcslashes ($str, "'");
Echo $str;


String padding
$str = "Dfsfs";
$str =str_pad ($str, ten, "V"); If the $str is not long enough, it will be automatically filled with V
Echo $str;


string comparison
$str 1= "AAAA";
$str 2= "AAAAAAA";
Echo strcmp ($str 1, $str 2);


All output from the location following the occurrence of the string
$str 1= "a";
$str 2= "DSFHFAVVVN";
Echo Strstr ($str 2, $str 1);


Replace character
$str 2= "DSFHFAVVVN";
$str =str_replace ("A", "B", $str 2); Case sensitive
$str =str_ireplace ("A", "B", $str 2); This substitution is case insensitive
$str =substr_replace ("A", "G", $str 2); The original value is only truncated a, plus g to form a new string
Echo $str;


Get address bar parameters, print an array

Parse_str ("id=5&cid=7", $myArray);
Print_r ($myArray);
Print_r ($_server); Get the server array


If the address bar has parameters, get the Address bar parameter and print the array
$str =$_server[' query_string '];
Parse_str ($str, $arr);
Print_r ($arr);


Output URL and Address bar parameters
$url = "Connection Address";

echo $url;
$arr =parse_url ($url);
Print_r ($arr);

Print array
$arr =array (0=> "My Car",1=> "is the Red",2=> "BMW");
Print_r ($arr);
echo $arr [1]; One of the values of the output array

See if there is a value in the array to find
$arr =array (0=> "BMW",1=> "Rolls-Royce",2=> "Mercedes");
$str = "BMW";
Echo In_array ($str, $arr); There is a return value of 1, there is no return value 0

To view the key values of an array
$arr =array (a=> "BMW",b=> "Rolls-Royce",c=> "Mercedes");
$str =array_keys ($arr);
Print_r ($STR);

Iterating through an array
$arr =array ("a" = "BMW", "b" = "Mercedes", "C" and "Rolls-Royce");
foreach ($arr as $key = = $value) {
Echo $key; Output key
echo $value. " <br> ";//Output key value
}

Current () function, next () function
$arr =array ("a" = "BMW", "b" = "Mercedes", "C" and "Rolls-Royce");
echo Key ($arr);
Echo current ($arr). "  <br> "; Returns the value of the array element with the current pointer position
Next ($arr);//Returns the value of the array element at the next position of the current pointer, moving the pointer down one
echo Key ($arr);
echo Key ($arr);

$arr =array ("a" = "BMW", "b" = "Mercedes", "C" and "Rolls-Royce");
$num =count ($arr);
for ($i =1; $i <= $num; $i + +) {
echo Key ($arr);
Echo current ($arr);
echo "<br>";
Next ($arr);
}

End () function, prev () function
$arr =array ("a" = "BMW", "b" = "Mercedes", "C" and "Rolls-Royce");
$num =count ($arr);
Echo End ($arr); Used to point the array pointer to the last element and return the value of the element
Prev ($arr); Returns an array element on the current pointer's previous position
Echo current ($arr);

Increase the value of an array element

Increase
$arr =array ("a" = "BMW", "b" = "Mercedes", "C" and "Rolls-Royce");
Array_push ($arr, "Audi", "Pentium"); Increase the value of an array element
Print_r ($arr);

$arr =array ("a" = "BMW", "b" = "Mercedes", "C" and "Rolls-Royce");
Array_unshift ($arr, "bubble face"); Used to add to the first element of an array
Print_r ($arr);

Array fill and remove repeating array elements
$arr =array ("a" = "BMW", "b" = "Mercedes", "C" and "Rolls-Royce");
$arr =array_pad ($arr, 10, "good");//array fill <br/>
$arr =array_unique ($arr); Remove duplicate elements from an array
Print_r ($arr);

Remove
$arr =array ("a" = "BMW", "b" = "Mercedes", "C" and "Rolls-Royce");
echo Array_pop ($arr); Remove the last element and reset the array
Print_r ($arr); The last array element is taken out

$arr =array ("a" = "BMW", "b" = "Mercedes", "C" and "Rolls-Royce");
for ($i =1; $i <=3; $i + +) {
echo Array_pop ($arr);
echo "<br>";
}

Ascending order ordering of array elements
$arr =array ("A", "D", "E", "C", "B");
Sort ($arr); Ascending
Rsort ($arr); The order
Print_r ($arr);

To synthesize multiple array groups into a new array
$arr =array ("A", "D", "E", "C", "B");
$arr 2=array ("K", "B", "N", "V", "U");
$arr =array_merge ($arr, $arr 2);//group to synthesize a new array
$arr =array_unique ($arr);//Remove duplicate elements from the new array
Sort ($arr); Sort Ascending
Print_r ($arr);

PHP string functions and array functions

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.