




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、 .PHP網站開發必用知識點總結,函數一覽,開發必背魔術方法 _construct() 當實例化一個對象的時候,這個對象的這個方法首先被調用。 _destruct() 當刪除一個對象或對象操作終止的時候,調用該方法。 _get() 當試圖讀取一個并不存在的屬性的時候被調用。 _set() 當試圖向一個并不存在的屬性寫入值的時候被調用。 _call() 當試圖調用一個對象并不存在的方法時,調用該方法。 _toString() 當打印一個對象的時候被調用 _clone() 當對象被克隆時,被調用_isset()_unset()_autoload($classname)_sleep()_wakeup
2、()系統常量 _FILE_ 當前文件名 _LINE_ 當前行數 _FUNCTION_ 當前函數名 _CLASS_ 當前類名 _METHOD_ 當前對象的方法名 PHP_OS 當前系統 PHP_VERSION php版本 DIRECTORY_SEPARATOR 根據系統決定目錄的分隔符 / PATH_SEPARATOR 根據系統決定環境變量的目錄列表分隔符 ; : E_ERROR 1 E_WARNING 2 E_PARSE 4 E_NOTICE 8 M_PI 3.141592 $_SERVER $_ENV 執行環境提交至腳本的變量 $_GET $_POST $_REQUEST $_FILES $
3、_COOKIE $_SESSION $_GLOBALS輸出echo /Output one or more stringsprint /Output a stringprint_r() /打印關于變量的易于理解的信息。var_dump() /打印變量的相關信息var_export() /輸出或返回一個變量的字符串表示printf("%.1f",$num) /Output a formatted string sprintf() /Return a formatted string編碼轉換string mb_convert_encoding ( string $str , s
4、tring $to_encoding , mixed $from_encoding )iconv();時間date_default_timezone_set("PRC");date("Y-m-d H:i:s");time();date("Y-m-d H:i:s",time()+3600)ini_set('date.timezone', 'PRC');msec sec microtime() 以秒返回時間戳 explode(' ', microtime()錯誤處理1/0error_repo
5、rting(E_ALL) 顯示所有錯誤 error_reporting(0)trigger_error("Cannot divide by zero", E_USER_ERROR);trythrow new Exception("執行失敗");catch (Exception $ex)echo $ex;字符串處理string trim("eee ") trim ('ffffe','e') /ltrim rtrimarray explode(".", "fff.ff.f&qu
6、ot;) 按指定字符切割string implode(".", $array) 別名:join 把數組值數據按指定字符連接起來array str_split("eeeeeeee",4) 按長度切割字符串 array split("-","fff-ff-f") 按指定字符切割int strlen('ffffffff') 取字符長度string substr ( string $string , int $start , int $length ) substr($a,-2, 2) 截取字符int su
7、bstr_count($text, 'is') 字符串出現的次數string strstr($text, 'h') 第一次出現h后的字符串 /別名:strchrint strpos($text, 'h') 第一次出現h的位置strrpos();最后一次出現h的位置str_replace('a', 'ttt', $t) 把$t里的'a'替換為'ttt' strtr($t,'is','ppp') 把$t中'is'替換成'ppp
8、9; strtr("hi all, I said hello", array("hello" => "hi") 把'hello'轉換成'hi'string md5_file('1.txt',false) 文件數據md5加密int strcmp(string str1, string str2) 字符串比較int strcasecmp(string str1, string str2) 忽略大小寫 string str_pad($i, 10, "-=", STR_
9、PAD_LEFT) 在原字符左邊補'-=',直到新字符串長度為10 STR_PAD_RIGHT STR_PAD_BOTHstring str_repeat('1', 5) 重復5個1 void parse_str('id=11'); echo $id; 將字串符解析為變量array preg_grep("/(d+)?.d+$/", array(11.2,11,11.2) 匹配數據array preg_split ("/s,+/", "hypertext language,programming&q
10、uot;); 按指定的字符切割array pathinfo(string path , int options) 返回文件路徑的信息string basename ( string path , string suffix ) 返回路徑中的文件名部分string dirname ( string path ) $_SERVERPHP_SELF 返回路徑中的目錄部分string nl2br("foo isn'tn bar") "foo isn't<br> bar" 把換行轉成<br>string chr ( int
11、ascii ) *mixed str_word_count ( string string , int format , string charlist )string str_shuffle ('abc') 打亂字符串順序string strrev($str) * 翻轉一個字符串string strtolower($str) * 將字符串 $str 的字符全部轉換為小寫的string strtoupper($str) * 將字符串 $str 的字符全部轉換為大寫的string ucfirst ($str) * 將字符串 $str 的第一個單詞的首字母變為大寫。string u
12、cwords($str) * 將字符串 $str 的每個單詞的首字母變為大寫。string addslashes("I'm") I'm 使用反斜線引用字符串 這些字符是單引號(')、雙引號(")、反斜線()與 NUL(NULL 字符)string stripcslashes("I'm") I'm 將用addslashes()函數處理后的字符串返回原樣strip_tags("<p>tt</p>", '<p>') 去除html、xml、p
13、hp標記,第二個參數用來保留標記string urlencode(string str)string urldecode(string str)string htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES) 轉換特殊字符為HTML字符編碼<a href='test'>Test</a>ENT_COMPAT 對雙引號進行編碼,不對單引號進行編碼ENT_QUOTES 對單引號和雙引號進行編
14、碼ENT_NOQUOTES 不對單引號或雙引號進行編碼string htmlentities('<p>ff</p>', ENT_QUOTES) 轉換特殊字符為HTML字符編碼,中文會轉成亂碼數組處理int count( mixed var , int mode ) 別名:sizeof() 取數組長度 string implode(".", $array) 別名:join 把數組值數據按指定字符連接起來array explode(".", "fff.ff.f") 按指定字符切割array rang
15、e(0, 6, 2) 返回數組 array(0,2,4,6) 第一個參數為起使數,第二個參數為結束數,第三個參數為數據增加步長int array_push($a, "3", 1) 把'3'、'1'壓入$a,將一個或多個單元壓入數組的末尾(入棧),第二個參數開始就是壓入的數據void unset ( mixed var , mixed var , . )array array_pad ($a, 5, 's')用's'將數組填補到指定長度bool shuffle ( array $array ) 將數組打亂mixe
16、d array_rand ( array input , int num_req )從數組中隨機取出一個或多個單元的索引或鍵名array array_count_values ( array input )統計數組中所有的值出現的次數array array_combine ( array keys, array values ) 創建一個數組,用一個數組的值作為其鍵名,另一個數組的值作為其值bool array_key_exists ( mixed key, array search )檢查給定的鍵名或索引是否存在于數組中mixed array_search ( mixed needle, a
17、rray haystack , bool strict )在數組中搜索給定的值,如果成功則返回相應的鍵名bool is_array ( mixed var )bool in_array ( mixed needle, array haystack , bool strict )檢查數組中是否存在某個值number array_sum ( array array )計算數組中所有值的和array array_unique ( array array )移除數組中重復的值mixed reset ( array &array )將數組的內部指針指向第一個單元mixed current ( a
18、rray &array )mixed next ( array &array )mixed prev ( array &array )mixed end ( array &array )mixed key ( array &array )array array_keys ( array input , mixed search_value , bool strict ) 返回數組中所有的鍵名array array_values ( array input ) 返回數組中所有的值bool print_r ( mixed expression , bool
19、return )void var_dump ( mixed expression , mixed expression , . )int array_unshift ( array &array, mixed var , mixed . )在數組開頭插入一個或多個單元mixed array_shift ( array &array )將數組開頭的單元移出數組mixed array_pop ( array &array )將數組最后一個單元彈出(出棧)array array_splice ( array $input, int offset , int length ,
20、array replacement ) 把數組中的一部分去掉并用其它值取代array array_merge ( array array1 , array array2 , array . )合并一個或多個數組array array_flip ( array trans )交換數組中的鍵和值int extract( array var_array , int extract_type , string prefix ) 從數組中將變量導入到當前的符號表array compact ( mixed varname , mixed . ) 建立一個數組,包括變量名和它們的值bool sort ( a
21、rray &array , int sort_flags )從最低到最高重新安排bool natsort($a)用“自然排序”算法對數組排序 bool rsort ( array &array , int sort_flags )對數組進行逆向排序(最高到最低)bool asort ( array &array , int sort_flags )對數組進行排序并保持索引關系bool arsort ( array &array , int sort_flags ) 對數組進行逆向排序并保持索引關系bool ksort ( array &array , i
22、nt sort_flags )對數組按照鍵名排序bool krsort ( array &array , int sort_flags )對數組按照鍵名逆向排序array array_filter ( array input , callback callback ) 用回調函數過濾數組中的單元bool array_walk ( array &array, callback funcname , mixed userdata ) 對數組中的每個成員應用用戶函數array array_map ( callback callback, array arr1 , array . )將
23、回調函數作用到給定數組的單元上array array_fill ( int start_index, int num, mixed value ) 用給定的值填充數組 array_fill(5, 3, 'a')->array(5=>'a',6=>'a',7=>'a')array array_chunk ( array input, int size , bool preserve_keys )將一個數組分割成多個其他 isset() 變量是否存在 boolean empty() 檢查變量是否存在,并判斷值是
24、否為非空或非0 void unset() 銷毀變量 header('Content-Type: text/html; charset=utf-8');method_exists($obj, $method)判斷對象的方法是否可用file_exists($file)判斷文件是否存在function_exists();class_exists($class_name); gettype();獲取數據類型 set_magic_quotes_runtime() 0 for off, 1 for on 當遇到反斜桿、單引號,將會自動加上一個反斜桿,保護系統和數據庫的安全ini_set()
25、;安全function strReplace($str) $strResult = $str; if(!get_magic_quotes_gpc()/判斷設置是否開啟 $strResult = addslashes($strResult);/轉換sql語句特殊字符 return $strResult;function quotes($content)/如果magic_quotes_gpc=Off,那么就開始處理if (!get_magic_quotes_gpc()/判斷$content是否為數組if (is_array($content)/如果$content是數組,那么就處理它的每一個單無f
26、oreach ($content as $key=>$value)$content$key = addslashes($value);else/如果$content不是數組,那么就僅處理一次addslashes($content);/返回$contentreturn $content;smarty模板引擎將不分析<!-literal-><script>function t() </script><!-/literal->讀取配置文件<!-config_load file="config.s"-><!-#
27、site_url#-><!-$smarty.config.site_url->引入文件<!-include file="index2.html"-><!-include_php file="/path/to/load_nav.php"-> $trusted_dir 指定目錄下的文件捕獲模板輸出的數據<!-capture name='eee'->fffffffff<!-/capture-><!-$smarty.capture.eee->循環<section
28、name=loop loop=$News_IN><$News_INloop.NewsID></section><!-section name=t loop=$data-><tr><td><!-$datat.username-></td></tr><!-/section-><foreach from=$newsArray item=newsID key=k>新聞編號:<$newsID.newsID><br>新聞內容:<$newsID.news
29、Title><br><hr></foreach>判斷<!-if true->1111<!-else->22222222<!-/if->時間$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"%Y年%m月%d日 亂碼<!-$smarty.now|date_format:"%Y年%m月%d日 %H時%M分%S秒"->修改插件:plugins/modifier.date_format.php$format = mb_convert_enc
30、oding($format,'gbk','utf-8');return mb_convert_encoding(strftime($format, $timestamp),'utf-8','gbk');局部不緩存html:<!-$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"-><!-cacheless a="aaa" b="bbbb"-><!-$smarty.now|date_format:"
31、%Y-%m-%d %H:%M:%S"-><!-/cacheless->php:$smarty->register_block('cacheless', 'smarty_block_dynamic', false);/true:緩存,false:不緩存function smarty_block_dynamic($param, $content, &$smarty)return $content;php:function insert_kk()/方法名前必須有"insert"return date(
32、9;Y-m-d H:i:s');html:<!-insert name="kk"->自定義方法注冊方法php$smarty->register_function('test1', 'test');function test($p)return 'ffffffffff'html:<!-test1 name="ff"->-方法自定義插件文件方式定義方法function.test.php 文件存在plugins目錄下,方法名:smarty_function_test($par
33、ams, &$smarty)function smarty_function_test($params, &$smarty)return 'fff'html調用:<!-test name='aa' p='ff'->-插入方法插件文件:insert.kk.php文件存于plugins目錄下function smarty_insert_kk()return date('Y-m-d H:i:s');php:function insert_kk()/方法名前必須有"insert"return
34、 date('Y-m-d H:i:s');html:<!-insert name="kk"->-管道符自定義方法插件文件方式定義方法modifier.test.php 文件存在于plugins目錄下,方法名: function smarty_modifier_test($str, $str2)function smarty_modifier_test($str, $str2)return $str.$str2;html調用:<!-'ff'|test:'tt'->php:function eee($a)r
35、eturn 'ffffffffffffff'html:<!-''|eee->if語句eq相等,ne、neq不相等,gt大于gte、ge大于等于,lte、le 小于等于,not非, mod求模。is not div by是否能被某數整除,is not even是否為偶數,$a is not even by $b 即($a / $b) % 2 = 0is not odd是否為奇$a is not odd by $b即($a / $b) % 2 != 0XML saxxml:<-?xml version="1.0" encodin
36、g="utf-8"?-><books> <book> <author>Jack Herrington</author> <title>PHP Hacks</title> <publisher>O'Reilly</publisher> </book> <book> <author>Jack Herrington</author> <title>Podcasting Hacks</title> &l
37、t;publisher>O'Reilly</publisher> </book> <book> <author>作者</author> <title>標題</title> <publisher>出版者</publisher> </book></books>php: $g_books = array(); $g_elem = null; function startElement( $parser, $name, $attrs ) global $g_
38、books, $g_elem; if ( $name = 'BOOK' ) $g_books = array(); $g_elem = $name; function endElement( $parser, $name ) global $g_elem; $g_elem = null; function textData( $parser, $text ) global $g_books, $g_elem; if ( $g_elem = 'AUTHOR' | $g_elem = 'PUBLISHER' | $g_elem = 'TITL
39、E' ) $g_books count( $g_books ) - 1 $g_elem = $text; $parser = xml_parser_create(); xml_set_element_handler( $parser, "startElement", "endElement" ); xml_set_character_data_handler( $parser, "textData" ); $f = fopen( '1.xml', 'r' ); while($data = fre
40、ad( $f, 4096 ) xml_parse( $parser, $data ); xml_parser_free( $parser ); foreach( $g_books as $book ) echo $book'TITLE'." - ".$book'AUTHOR'." - " echo $book'PUBLISHER'."<br>" DomDocument()xml:<-?xml version="1.0" encoding="
41、utf-8"?-><books> <book> <author>Jack Herrington</author> <title>PHP Hacks</title> <publisher>O'Reilly</publisher> </book> <book> <author>Jack Herrington</author> <title>Podcasting Hacks</title> <publis
42、her>O'Reilly</publisher> </book> <book> <author>作者</author> <title>標題</title> <publisher>出版者</publisher> </book></books>php讀取: $doc = new DOMDocument(); $doc->load( "1.xml"); $books = $doc->getElementsByTagName(
43、 "book" ); foreach( $books as $book ) $authors = $book->getElementsByTagName( "author" ); $author = $authors->item(0)->nodeValue; $publishers = $book->getElementsByTagName( "publisher" ); $publisher = $publishers->item(0)->nodeValue; $titles = $book-&g
44、t;getElementsByTagName( "title" ); $title = $titles->item(0)->nodeValue; echo "$title - $author - $publisher<br>" php生成:$books = array();$books = array('title' => 'PHP Hacks','author' => 'Jack Herrington','publisher' =>
45、 "O'Reilly");$books = array('title' => 'Podcasting Hacks','author' => 'Jack Herrington','publisher' => "O'Reilly");$doc = new DOMDocument();$doc->formatOutput = true;$r = $doc->createElement( "books" );$doc-
46、>appendChild( $r );foreach( $books as $book )$b = $doc->createElement( "book" );$author = $doc->createElement( "author" );$author->appendChild($doc->createTextNode( $book'author' );$b->appendChild( $author );$title = $doc->createElement( "title&q
47、uot; );$title->appendChild($doc->createTextNode( $book'title' );$b->appendChild( $title );$publisher = $doc->createElement( "publisher" );$publisher->appendChild($doc->createTextNode( $book'publisher' );$b->appendChild( $publisher );$r->appendChild(
48、$b );echo $doc->saveXML();echo $doc->save('222.xml'); SimpleXMLxml:<books> <book> <author>Jack Herrington</author> <title>PHP Hacks</title> <publisher>O'Reilly</publisher> </book></books>php:$xml = new SimpleXMLElement(
49、9;1.xml', NULL, TRUE);echo $xml->book0->author."_".$xml->book0->title."_".$xml->book0->publisher;正則ereg系列的正則表達式不需要定屆符,preg系列的才需要,并且定界符可以自己選擇,只有前后一對就行,比如我們一般使用/符號,但是如果里面有/需要匹配那么就需要使用/來表示,當/需要出現多次的時候,這樣就不方便,我們就可以使用其他的定界符,比如|正則特殊字符. + * ? $ ( ) = ! < > |
50、: 由原子(普通字符,如英文字符)、 元字符(有特殊功用的字符) 模式修正字符 一個正則表達式中,至少包含一個原子 全部符號解釋 將下一個字符標記為一個特殊字符、或一個原義字符、或一個 向后引用、或一個八進制轉義符。例如,'n' 匹配字符 "n"。'n' 匹配一個換行符。序列 '' 匹配 "" 而 "(" 則匹配 "("。 匹配輸入字符串的開始位置。如果設置了 RegExp 對象的 Multiline 屬性, 也匹配 'n' 或 'r'
51、 之后的位置。 $ 匹配輸入字符串的結束位置。如果設置了RegExp 對象的 Multiline 屬性,$ 也匹配 'n' 或 'r' 之前的位置。 * 匹配前面的子表達式零次或多次。例如,zo* 能匹配 "z" 以及 "zoo"。* 等價于0,。 + 匹配前面的子表達式一次或多次。例如,'zo+' 能匹配 "zo" 以及 "zoo",但不能匹配 "z"。+ 等價于 1,。 ? 匹配前面的子表達式零次或一次。例如,"do(es)?&quo
52、t; 可以匹配 "do" 或 "does" 中的"do" 。? 等價于 0,1。 n n 是一個非負整數。匹配確定的 n 次。例如,'o2' 不能匹配 "Bob" 中的 'o',但是能匹配 "food" 中的兩個 o。 n, n 是一個非負整數。至少匹配n 次。例如,'o2,' 不能匹配 "Bob" 中的 'o',但能匹配 "foooood" 中的所有 o。'o1,' 等價于
53、'o+'。'o0,' 則等價于 'o*'。 n,m m 和 n 均為非負整數,其中n <= m。最少匹配 n 次且最多匹配 m 次。例如,"o1,3" 將匹配 "fooooood" 中的前三個 o。'o0,1' 等價于 'o?'。請注意在逗號和兩個數之間不能有空格。 ? 當該字符緊跟在任何一個其他限制符 (*, +, ?, n, n, n,m) 后面時,匹配模式是非貪婪的。非貪婪模式盡可能少的匹配所搜索的字符串,而默認的貪婪模式則盡可能多的匹配所搜索的字符串。例如,對于
54、字符串 "oooo",'o+?' 將匹配單個 "o",而 'o+' 將匹配所有 'o'。 . 匹配除 "n" 之外的任何單個字符。要匹配包括 'n' 在內的任何字符,請使用象 '.n' 的模式。 (pattern) 匹配 pattern 并獲取這一匹配。所獲取的匹配可以從產生的 Matches 集合得到,在VBScript 中使用 SubMatches 集合,在JScript 中則使用 $0$9 屬性。要匹配圓括號字符,請使用 '(' 或
55、')'。 (?:pattern) 匹配 pattern 但不獲取匹配結果,也就是說這是一個非獲取匹配,不進行存儲供以后使用。這在使用 "或" 字符 (|) 來組合一個模式的各個部分是很有用。例如, 'industr(?:y|ies) 就是一個比 'industry|industries' 更簡略的表達式。 (?=pattern) 正向預查,在任何匹配 pattern 的字符串開始處匹配查找字符串。這是一個非獲取匹配,也就是說,該匹配不需要獲取供以后使用。例如,'Windows (?=95|98|NT|2000)' 能匹
56、配 "Windows 2000" 中的 "Windows" ,但不能匹配 "Windows 3.1" 中的 "Windows"。預查不消耗字符,也就是說,在一個匹配發生后,在最后一次匹配之后立即開始下一次匹配的搜索,而不是從包含預查的字符之后開始。 (?!pattern) 負向預查,在任何不匹配 pattern 的字符串開始處匹配查找字符串。這是一個非獲取匹配,也就是說,該匹配不需要獲取供以后使用。例如'Windows (?!95|98|NT|2000)' 能匹配 "Windows 3.1
57、" 中的 "Windows",但不能匹配 "Windows 2000" 中的 "Windows"。預查不消耗字符,也就是說,在一個匹配發生后,在最后一次匹配之后立即開始下一次匹配的搜索,而不是從包含預查的字符之后開始 x|y 匹配 x 或 y。例如,'z|food' 能匹配 "z" 或 "food"。'(z|f)ood' 則匹配 "zood" 或 "food"。 xyz 字符集合。匹配所包含的任意一個字符。例如,
58、'abc' 可以匹配 "plain" 中的 'a'。 xyz 負值字符集合。匹配未包含的任意字符。例如, 'abc' 可以匹配 "plain" 中的'p'。 a-z 字符范圍。匹配指定范圍內的任意字符。例如,'a-z' 可以匹配 'a' 到 'z' 范圍內的任意小寫字母字符。 a-z 負值字符范圍。匹配任何不在指定范圍內的任意字符。例如,'a-z' 可以匹配任何不在 'a' 到 'z' 范圍內的任意字符。 b 匹配一個單詞邊界,也就是指單詞和空格間的位置。例如, 'erb' 可以匹配"never" 中的 'er',但不能匹配 "verb"
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 村辦酒禮堂管理制度
- 村干部出差管理制度
- 村民實踐站管理制度
- 村社區考勤管理制度
- 村置業公司管理制度
- 來藥店流程管理制度
- 校園外車輛管理制度
- 校園童玩角管理制度
- 植入性器械管理制度
- 水暖科安全管理制度
- 內蒙古自治區中小學職稱評價標準條件
- GB/T 29047-2021高密度聚乙烯外護管硬質聚氨酯泡沫塑料預制直埋保溫管及管件
- 某射擊館照明平面回路設計及智能照明控制分析
- 植物檢疫性病毒病害
- 血管外科常見疾病課件
- 農村公共管理復習資料
- 人教版道德與法治八年級下冊期末測試卷--含答案-八下道德期末試卷人教版
- Q∕GDW 12067-2020 高壓電纜及通道防火技術規范
- 2020-2021廣東二建繼續教育試題及答案
- 幼兒園繪本:《小交通員》 紅色故事
- 工程機械液壓系統設計與計算--12噸起重機
評論
0/150
提交評論