php常用函數

來源:互聯網
上載者:User

標籤:產生   sub   範圍   php   www   拼接   ima   count   strcmp   

1、隨機數和時間

echo rand();  //隨機數產生器
echo rand(0,10); //產生某個範圍內的隨機數

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>無標題文檔</title></head><body><?php echo rand(0,10);?></body></html>

輸出的結果是:

0到10之間的隨機數



echo time();  //取目前時間戳
echo date("Y-m-d H:i:s",1381253766); //格式化顯示時間
echo strtotime("2013-10-09 01:36:06"); //將字串轉換為時間戳記

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>無標題文檔</title></head><body><?php echo time();  //取目前時間戳echo date("Y-m-d H:i:s",1381253766); //格式化顯示時間echo strtotime("2013-10-09 01:36:06"); //將字串轉換為時間戳記?></body></html>

輸出的結果:

 



2.字串函數
$str = "Hello|World|ni|hao";
$attr = array("aa","bb","cc","dd");

echo strlen($str); //取字串的長度
var_dump(strcmp($str,"hello world")); //比較兩個字串
echo strtolower($str); //轉小寫
echo strtoupper($str); //轉大寫

var_dump(explode("|",$str)); //拆分字串,返回數組
echo implode("--",$attr); //將數組元素拼接成一個字串

echo substr_replace($str,"***",0,5); //替換指定位置的字串
//echo str_replace("|","***",$str); //尋找替換
//echo substr($str,0,5); //截取字串

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>無標題文檔</title></head><body><?php $str = "Hello|World|ni|hao";$attr = array("aa","bb","cc","dd");echo strlen($str); //取字串的長度var_dump(strcmp($str,"hello world")); //比較兩個字串echo strtolower($str); //轉小寫echo strtoupper($str); //轉大寫var_dump(explode("|",$str)); //拆分字串,返回數組echo implode("--",$attr); //將數組元素拼接成一個字串echo substr_replace($str,"***",0,5); //替換指定位置的字串echo str_replace("|","***",$str); //尋找替換echo substr($str,0,5); //截取字串?></body></html>

輸出的結果:

 


//3.Regex
$str =<<<A
內容
A;

echo preg_replace("/\d/","#",$str); //替換
var_dump(preg_split("/\d/",$str)); //拆分
preg_match("/\d/",$str,$arr); //匹配第一個滿足正則的字串
preg_match_all("/\d/",$str,$arr); //匹配所有滿足正則的字串
var_dump($arr);

preg_match_all("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/",$str,$arr);

var_dump($arr);

4.數組方法
$attr = array(1,2,3,4,5,1);
var_dump(in_array(6,$attr)); //判斷某個值是否在數組裡面
var_dump(array_reverse($attr)); //翻轉數組
echo count($attr); //取數組長度
var_dump(array_unique($attr)); //去重
unset($attr[1]); //刪除數組的元素
var_dump(array_values($attr)); //重新索引
var_dump(array_merge($attr,array(5,6))); //合并數組
array_push($attr,"hello"); //向數組裡面添加一個元素,返回索引

php常用函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.