PHP之主要函數

來源:互聯網
上載者:User
PHP之重要函數
積少成多——作者:zccst

8 , string base64_encode(string data);
本函數將字串以 MIME BASE64 編碼。此編碼方式可以讓中文字或者圖片也能在網路上順利傳輸。在 BASE64 編碼後的字串只包含英文字母大小寫、阿拉伯數字、加號與反斜線,共 64 個基底字元,不包含其它特殊的字元,因而才取名 BASE64。編碼後的字串比原來的字串長度再加 1/3 左右。更多的 BASE64 編碼資訊可以參考 RFC2045 檔案之 6.8 節。

7,similar_text
similar_text() Function Compute兩個字串的匹配字元的數目,也可以計算兩個字串的相似性(以百分比計)。
參數 描述
string1 必需。規定要比較的第一個字串。
string2 必需。規定要比較的第二個字串。
percent 可選。規定供儲存百分比相似性的變數名。
similar_text("Hello World","Hello Peter",$percent);echo $percent;

輸出:63.6363636364


6,spl_autoload_register('autoloader');
function autoloader($className){  if(file_exists(dirname(__FILE__)."/$className.php")){        include dirname(__FILE__)."/$className.php";        if(class_exists($className,false)){            return true;        }    }  return false;}

批註:
bool class_exists ( string $class_name [, bool $autoload = true ] )
第二個參數:Whether or not to call __autoload by default.
只有當第二個參數為false時,才能不開啟autoload,以節省開支。
__autoload機制:
參閱另一篇文章:php autoload機制。


5,addslashes()和stripslashes()
addslashes() 函數在指定的預定義字元前添加反斜線。 這些預定義字元是: 單引號 (') 雙引號 (") 反斜線 (\) NULL
$str = "Is your name O'reilly?";// Outputs: Is your name O\'reilly?echo addslashes($str);

stripslashes() 函數刪除由 addslashes() 函數添加的反斜線

4,get_magic_quotes_gpc()
int get_magic_quotes_gpc ( void )
Returns the current configuration setting of magic_quotes_gpc
Keep in mind that attempting to set magic_quotes_gpc at runtime will not work.
For more information about magic_quotes, see this security section.
批註:取得 PHP 環境變數 magic_quotes_gpc(GPC, Get/Post/Cookie) 的值。返回 0 表示關閉本功能;返回 1 表示本功能開啟。
當 magic_quotes_gpc 開啟時,所有的 ' (單引號), " (雙引號), \ (反斜線) and Null 字元會自動轉為含有反斜線的逸出字元。


當magic_quotes_gpc = On時,系統會自動處理單引號等問題,用不用addslashes()和stripslashes()都沒關係,但是如果添加資料時用了addslashes(),那麼顯示資料時必須要stripslashes()

當magic_quotes_gpc = Off時,系統不會處理單引號等問題,所以插入資料時必須要使用addslashes(),顯示資料時則不需要使用stripslashes()。

既然有了分析,做程式時要怎麼辦呢?根據以上兩種情況,可得:

不管magic_quotes_gpc是On還是Off,咱添加資料時都用addslashes(),當On時,必須使用stripslashes(),Off時則不能用stripslashes()。

如何判斷On還是Off呢?用get_magic_quotes_gpc()。

最後舉例:
//1,存入資料庫$Content=addslashes(”這裡面是資料,不管有沒單引號或者還是變數”);//插入資料到資料庫,代碼省略//2,從資料庫取$Content=”從資料庫讀取的資料”;if(get_magic_quotes_gpc()){  $Content=stripslashes($Content); }echo $Content;/************  例子2  **************/echo get_magic_quotes_gpc();         // 1echo $_POST['lastname'];             // O\'reillyecho addslashes($_POST['lastname']); // O\\\'reillyif (get_magic_quotes_gpc()) {    $lastname = stripslashes($_POST['lastname']);}else {    $lastname = $_POST['lastname'];}// If using MySQL$lastname = mysql_real_escape_string($lastname);echo $lastname; // O\'reilly$sql = "INSERT INTO lastnames (lastname) VALUES ('$lastname')";



3,get_file_contents($fileName)
把檔案讀入一個字串
file_get_contents() 函數是用來將檔案的內容讀入到一個字串中的首選方法。

file_put_contents(string $filename , string $data [, int $flags [, resource $context ]] )
將一個字串寫入檔案

2,str_replace(array('\r\n'), array("\n"), $str)


1,strip_tags($v)
原型:string strip_tags ( string $str [, string $allowable_tags ] )

解讀:This function tries to return a string with all NUL bytes, HTML and PHP tags stripped from a given str. It uses the same tag stripping state machine as the fgetss() function.

舉例:
$text = '

Test paragraph.

Other text';echo strip_tags($text);echo "\n";// Allow

and echo strip_tags($text, '

');


輸出:
Test paragraph. Other text

Test paragraph.

Other text



  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.