PHP裏有非常豐富的內置函數,很多我們都用過,但仍有很多的函數我們大部分人都不熟悉,可它們卻十分的有用。這篇文章裏,我列舉了一些鮮爲人知但會讓你眼睛一亮的PHP函數。 levenshtein() 你有沒有經歷過需要知道兩個單詞有多大的不同的時候,這個函數就是來幫你解決這個問題的。
PHP裏有非常豐富的內置函數,很多我們都用過,但仍有很多的函數我們大部分人都不熟悉,可它們卻十分的有用。這篇文章裏,我列舉了一些鮮爲人知但會讓你眼睛一亮的PHP函數。
levenshtein()
你有沒有經歷過需要知道兩個單詞有多大的不同的時候,這個函數就是來幫你解決這個問題的。它能比較出兩個字符串的不同程度。
用法:
1
2
3
4
5
6
|
<?php $str1 = "carrot" ; $str2 = "carrrott" ; echo levenshtein( $str1 , $str2 ); //Outputs
2 ?> |
Source: http://php.net/manual/en/function.levenshtein.php
get_defined_vars()
這是一個在debug調試時非常有用的函數。這個函數返回一個多維數組,裏面包含了所有定義過的變量。
用法:
1
2
3
|
<?php print_r(get_defined_vars()); ?> |
Source: http://php.net/manual/en/function.get-defined-vars.php
php_check_syntax()
這個函數非常的有用,可以用來檢查PHP的語法是否正確。出於技術上的原因,從PHP 5.05開始,這個函數被刪除了。
用法:
1
2
3
4
5
6
7
8
9
|
<?php $error_message = "" ; $filename = "./php_script.php" ; if (!php_check_syntax( $filename ,
& $error_message ))
{ echo "Errors
were found in the file $filename: $error_message" ; } else { echo "The
file $filename contained no syntax errors" ; } ?> |
Source: http://www.php.net/manual/en/function.php-check-syntax.php
ignore_user_abort()
這個函數用來拒絕瀏覽器端用戶終止執行腳本的請求。正常情況下客戶端的退出會導致服務器端腳本停止運行。
用法:
1
2
3
|
<?php ignore_user_abort(); ?> |
Source: http://www.php.net/manual/en/function.ignore-user-abort.php
highlight_string()
當你想把PHP代碼顯示到頁面上時,highlight_string()函數就會顯得非常有用。這個函數會把你提供的PHP代碼用內置的PHP語 法突出顯示定義的顏色高亮顯示。這個函數有兩個參數,第一個參數是一個字符串,表示這個字符串需要被突出顯示。第二個參數如果設置成TRUE,這個函數就 會把高亮後的代碼當成返回值返回。
用法:
1
2
3
|
<?php highlight_string( '
<?php phpinfo(); ?>' ); ?> |
Source: http://php.net/manual/en/function.highlight-string.php
highlight_file
這是一個非常有用的PHP函數,它能返回指定的PHP文件,並按照語法語義用高亮顏色突出顯示文件內容。其中的突出顯示的代碼都是用HTML標記處理過的。
用法:
1
2
3
|
<?php highlight_file( "php_script.php" ); ?> |
Source: http://www.php.net/manual/en/function.highlight-file.php
php_strip_whitespace
這個函數也跟前面的show_source()函數相似,但它會刪除文件裏的註釋和空格符。
用法:
1
2
3
|
<?php echo php_strip_whitespace( "php_script.php" ); ?> |
Source: http://www.php.net/manual/en/function.php-strip-whitespace.php
get_browser
這個函數會讀取browscap.ini文件,返回瀏覽器兼容信息。
用法:
1
2
3
4
5
|
<?php echo $_SERVER [ 'HTTP_USER_AGENT' ]; $browser =
get_browser(); print_r( $browser ); ?> |
Source: http://www.php.net/manual/en/function.get-browser.php
memory_get_usage(),memory_get_peak_usage(),getrusage()
這些函數用來獲取內存和CPU使用情況,memory_get_usage()函數返回內存使用 量,memory_get_peak_usage()函數返回內存使用峯值,getrusage()返回CUP使用情況,在調試PHP代碼性能時,這些函 數會給你提供一些有用信息。但有一點請注意,在這些函數中Window上無效。
用法:
1
2
3
4
5
6
7
8
9
10
11
12
|
<?php echo "Initial:
" .memory_get_usage(). "
bytes \n" ; echo "Peak:
" .memory_get_peak_usage(). "
bytes \n" ; $data = getrusage (); echo "User
time: " . ( $data [ 'ru_utime.tv_sec' ]
+ $data [ 'ru_utime.tv_usec' ]
/ 1000000); echo "System
time: " . ( $data [ 'ru_stime.tv_sec' ]
+ $data [ 'ru_stime.tv_usec' ]
/ 1000000); ?> |
gzcompress(), gzuncompress()
這兩個函數用來壓縮和解壓字符串數據。它們的壓縮率能達到50% 左右。另外的函數 gzencode() 和 gzdecode() 也能達到類似結果,但使用了不同的壓縮算法。
用法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?php $string = "Lorem
ipsum dolor sit amet, consectetur adipiscing
elit. Nunc ut elit id mi ultricies adipiscing.
Nulla facilisi. Praesent pulvinar, sapien
vel feugiat vestibulum, nulla dui pretium orci, non
ultricies elit lacus quis ante. Lorem ipsum dolor sit
amet, consectetur adipiscing elit. Aliquam pretium
ullamcorper urna quis iaculis. Etiam ac massa sed
turpis tempor luctus. Curabitur sed nibh eu elit mollis
congue. Praesent ipsum diam, consectetur vitae ornare
a, aliquam a nunc. In id magna pellentesque tellus
posuere adipiscing. Sed non mi metus, at lacinia augue.
Sed magna nisi, ornare in mollis in, mollis sed
nunc. Etiam at justo in leo congue mollis. Nullam
in neque eget metus hendrerit scelerisque eu
non enim. Ut malesuada lacus eu nulla bibendum id
euismod urna sodales. "; $compressed =
gzcompress( $string ); $original =
gzuncompress( $compressed ); ?> |