Common functions in PHP

Source: Internet
Author: User

1. Random Number and time

1 echorand();      //随机数生成器
1 echorand(0,10);      //某个范围之间的随机数:第一个参数最小,第二个参数最大;例子是从0-10之间的随机数
1 echotime();      //当前的时间戳
1 echodate("Y",time());         //第一个参数是年份,不写第二个参数也是取得当前时间
1 echodate("Y-m-d H:i:s",1381248000);      //格式化显示时间:Y是指年m是指月份d代表天数H是小时i是分钟s秒数,时间戳  

Sometimes time may not be Chinese time, can be modified by two ways:

Method One:

1 date_default_timezone_set("PRC");

Method Two: Modify the date.timezone=ect/gmt-8 in PHP config file php.ini

1 echostrtotime("2013-10-09");      //反向转化: 将字符串转换为时间戳    2013-10-09返回的时间戳是1381248000


2. String functions

12 $str="hello world";echostrlen($str);    //字符串长度: 字符串

String $str= the length of "Hello World" is 11

12 $str="hello world";var_dump(strcmp($str,"hello world"));     //比较两个字符串:字符串,比较的字符串

The return value is 0; (The return value of both equals is 0;str1 less than str2 when the return value is <0,str1 greater than STR2)

12 $str="Hello world";echostrtolower($str);     //将字符串转换为小写

The return is to convert "H" to "H".

12 $str="hello world";echostrtoupper($str);      //将字符串转为大写

The return value is the conversion of "Hello World" to Hello World

12 $str="hello|world|ni|hao";var_dump(explode("|",$str));      //拆分字符串,拆完后返回数组

Results

12 $attr=array("aa","bb","cc","dd");echoimplode("&",$attr);      //将数组拼接一个字符串:想要拼接的符号,拼接的字符串

12 $str="hello|world|ni|hao";echosubstr_replace($str,"***",0,5);      //替换指定位置的字符串    $str,"***",0,5:字符串,替换后的符号,从几开始替换,长度

The return string is replaced with a character that starts at 0 and is 5 characters long.

  

12 $str="hello|world|ni|hao";echostr_replace("|","***",$str);     //字符串查找替换     "|","***",$str:查找的字符,替换成的字符,字符串  

To return is to replace all of the |

12 $str="hello|world|ni|hao";echosubstr($str,0,5);       //截取字符串    $str,0,5:字符串,截取位置,长度

The string that defines the string that starts at 0, which is the length of 5, is the Hello

3. Regular expressions

12 $str"2hello 5li 6lei";echopreg_replace("/\d/","#",$str);   //替换   "/\d/","#",$str: 正则表达式\d 数字,替换为#,字符串

The return value is the number in the string that is replaced by #

12 $str   "2hello 5li 6lei " var_dump (preg_split ( $str //split    regular expression, string
123 $str"2hello 5li 6lei";preg_match_all("/\d/",$str,$arr);    //正则表达式,定义的字符串,数组var_dump($arr);       //匹配满足所有正则的字符串

The value returned is the number 2,5,6

123 $str"2hello 5li 6lei";preg_match("/\d/",$str,$arr); var_dump($arr);     //匹配第一个满足正则的字符串

4. Array functions

12 $attrarray(1,2,3,4,5,1);var_dump(in_array(4,$attr));    //判断某个值是否在数组中   索引号,数组

The return value is ture, and if the array is exceeded, the return value is False

12 $attrarray(1,2,3,4,5,1);var_dump(array_reverse($attr));     //翻转数组

The data in the original array is reversed

12 $attrarray(1,2,3,4,5,1);echocount($attr);     //取数组长度

The return value is the length of the array 6

12 $attrarray(1,2,3,4,5,1);var_dump(array_unique($attr));     //去重

The return value is the same as in the array.

123 $attrarray(1,2,3,4,5,1);unset($attr[1]);      //索引号为1的元素var_dump($attr);    //删除数组元素

Deleted the number of index number 1, that is, 2 deleted

12 $attrarray(1,2,3,4,5,1);var_dump(array_values($attr));     //重新索引   打乱的索引重新排过

After re-indexing

12 $attrarray(1,2,3,4,5,1);var_dump(array_merge($attr,array(5,6)));     //合并数组   数组,想要合并的数组

123 $attrarray(1,2,3,4,5,1);array_push($attr,"hello");   //数组,添加的元素var_dump($attr);     //向数组中添加一个元素,返回索引

Example:

12345678910111213 $attr array(    array("n001","汉族"),    array("n002","回族"),    array("n003","维吾尔族")    ); echo"<select>"; foreach($attras$v){    echo"<option>{$v[1]}</option>";}    echo"</select>";

 

Common functions in PHP

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.