PHP Common functions

Source: Internet
Author: User
Tags echo date

1. Functions of random numbers and times
echo rand (); Random number generator
echo rand (0,10); Random number Generation Range

echo Time (); Take the current timestamp


echo Date ("y-m-d h:i:s"); Format time

Strtotime ("2013-03-04 01:35:06"); Date Time converted to timestamp

2. About String Classes

$str = "Hello World";
echo strlen ($STR); Take string length

Var_dump (strcmp ($str, "Hello World"); Comparison of two strings

echo Strtolower ($STR); Turn lowercase
echo Strtoupper ($STR); Turn capital

Splits a string, returns an array

$str = "Hello|world|ni|hao";
Var_dump (Explode ("|", $str));

Stitching array elements into a single string

$attr =array ("AA", "BB", "CC", "DD");
Echo Implode ("--", $attr);

Replaces a string at the specified position

Echo Substr_replace ($STR, "* * *", 0,5); The first one in parentheses is the string to be manipulated, the second is what to replace in the future, the third is the start position, and the fourth one is the length

echo str_replace ("|", "* * *", $STR); Find replacements

Echo substr ($str, 0,5); Intercept string

3. Regular expressions

Number replaced by #

$str = "2hello 5li 6lei";
Echo preg_replace ("/\d/", "#", $str);

Split

Var_dump (Preg_split ("/\d/", $str));

Match the first string that satisfies a regular

Preg_match ("/\d/", $str, $arr);
Var_dump ($arr);

Match all strings that satisfy the regular

Preg_match_all ("/\d/", $str, $arr);
Var_dump ($arr);

4. Array methods

Determine if a value is inside an array

$attr =array (1,2,3,4,5,1);
Var_dump (In_array (2, $attr));

Flipping an array

Var_dump (In_reverse ($attr));

Take array length

echo count ($att);

Go heavy

Var_dump (Array_unique ($attr));

To delete an array element

Unset ($attr [1]);

Re-index

Var_dump (Drray_values ($attr));

Merging arrays

Var_dump (Array_merge ($attr, Array (5,6)));

To add an element to the array, return the index

Array_push ($attr, "Hello");

Example:

$attr =array (

Array ("Noo1", "Han"),
Array ("Noo2", "Hui"),
Array ("Noo2", "Uighur clan")

);
Use the drop-down list to display

echo "<select>";

foreach ($attr as $v)
{
echo "<option>{$v [1]}</option>";
}
echo "</select>";

PHP Common 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.