10個實用的PHP代碼片段

jopen 10年前發布 | 26K 次閱讀 PHP PHP開發

作為一個PHP開發人員,經常收集一些代碼片段是非常有益的,以備將來使用。這些代碼片段可以節省您寶貴的時間,也提高您的工作效率。

 

從一個PHP數據生成 CSV 文件

這的確是一個很簡單的功能,從一個PHP數組生成一個.csv文件。此函數使用 fputcsv PHP 內置函數生成逗號分隔文件(.CSV)。該函數有3個參數:數據,分隔符和CSV enclosure 默認是雙引號。

function generateCsv($data, $delimiter = ',', $enclosure = '"') {
   $handle = fopen('php://temp', 'r+');
   foreach ($data as $line) {
           fputcsv($handle, $line, $delimiter, $enclosure);
   }
   rewind($handle);
   while (!feof($handle)) {
           $contents .= fread($handle, 8192);
   }
   fclose($handle);
   return $contents;
}

使用PHP對數據庫輸入進行惡意代碼清除

這是一個有用的PHP函數清理了所有的輸入數據,并刪除代碼注入的幾率。

function sanitize_input_data($input_data) {
    $input_data = trim(htmlentities(strip_tags($input_data,“,”)));
    if (get_magic_quotes_gpc())
    $input_data = stripslashes($input_data);
    $input_data = mysql_real_escape_string($input_data);
    return $input_data;
}

使用PHP解壓文件Unzip

這是一個非常方便的PHP函數從。zip文件解壓縮文件。它有兩個參數:第一個是壓縮文件的路徑和第二個是目標文件的路徑。

function unzip_file($file, $destination) {
    // create object
    $zip = new ZipArchive() ;
    // open archive
    if ($zip->open($file) !== TRUE) {
        die ('Could not open archive');
    }
    // extract contents to destination directory
    $zip->extractTo($destination);
    // close archive
    $zip->close();
    echo 'Archive extracted to directory';
}

從網頁提取的關鍵字

真的是一個非常有用的代碼片段從任何網頁中提取meta關鍵字。

$meta = get_meta_tags('http://www.emoticode.net/');
$keywords = $meta['keywords'];
// Split keywords
$keywords = explode(',', $keywords );
// Trim them
$keywords = array_map( 'trim', $keywords );
// Remove empty values
$keywords = array_filter( $keywords );

print_r( $keywords );

檢查服務器是否是 HTTPS

這個PHP代碼片段能夠讀取關于你服務器 SSL 啟用(HTTPS)信息。

if ($_SERVER['HTTPS'] != "on") { 
    echo "This is not HTTPS";
}else{
    echo "This is HTTPS";
}

在任意網頁顯示源代碼

這是簡單的PHP代碼,用于顯示任何網頁的源代碼包含行號。

$lines = file('http://google.com/');
foreach ($lines as $line_num => $line) { 
    // loop thru each line and prepend line numbers
    echo "Line #{$line_num} : " . htmlspecialchars($line) . "
\n";
}

 

創建數據的URI

因為我們知道,數據URI可以將圖像嵌入到HTML,CSS和JS以節省HTTP請求。這是一個非常實用的PHP代碼片段來創建數據URI。

function data_uri($file, $mime) {
  $contents=file_get_contents($file);
  $base64=base64_encode($contents);
  echo "data:$mime;base64,$base64";
}

取得一個頁面中的所有鏈接

通過使用此代碼段中,您可以很容易地提取任何網頁上的所有鏈接。

$html = file_get_contents('http://www.example.com');

$dom = new DOMDocument();
@$dom->loadHTML($html);

// grab all the on the page
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");

for ($i = 0; $i < $hrefs->length; $i++) {
       $href = $hrefs->item($i);
       $url = $href->getAttribute('href');
       echo $url.'
';
}

讓網頁標題變得對搜索引擎更友好

這是個非常有用的PHP函數,能夠根據網頁標題創建搜索引擎友好的URL。

function make_seo_name($title) {
    return preg_replace('/[^a-z0-9_-]/i', '', strtolower(str_replace(' ', '-', trim($title))));
}

使用PHP下載和保存遠程圖片在你的服務器中。

如果你想從一個特定的URL下載圖像并保存到服務器上,那么這個代碼片斷剛好滿足要求。

$image = file_get_contents('http://www.url.com/image.jpg');
file_put_contents('/images/image.jpg', $image); //save the image on your server

 

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!