亚洲国产日韩欧美在线a乱码,国产精品路线1路线2路线,亚洲视频一区,精品国产自,www狠狠,国产情侣激情在线视频免费看,亚洲成年网站在线观看

超級9個(gè)PHP代碼片段

時(shí)間:2025-12-21 04:19:14 php語言

超級實(shí)用的9個(gè)PHP代碼片段

  在開發(fā)網(wǎng)站、app或博客時(shí),代碼片段可以真正地為你節(jié)省時(shí)間。今天,讓我們一起來分享小編收集整理好的這些超級有用的PHP代碼片段。一起來看一看吧!

  1.創(chuàng)建數(shù)據(jù)URI

  數(shù)據(jù)URI在嵌入圖像到HTML / CSS / JS中以節(jié)省HTTP請求時(shí)非常有用,并且可以減少網(wǎng)站的加載時(shí)間。下面的函數(shù)可以創(chuàng)建基于$file的數(shù)據(jù)URI。

  function data_uri($file, $mime) {

  $contents=file_get_contents($file);

  $base64=base64_encode($contents);

  echo "data:$mime;base64,$base64";

  }

  2.合并JavaScript和CSS文件

  另一個(gè)可以盡量減少HTTP請求和節(jié)省頁面加載時(shí)間的好建議是:合并你的CSS和JS文件。雖然我更建議大家使用專用插件(例如minify),但使用PHP來合并文件也非常容易。我們來看一下:

  function combine_my_files($array_files, $destination_dir, $dest_file_name){

  if(!is_file($destination_dir . $dest_file_name)){ /pic/p>

  $content = "";

  foreach ($array_files as $file){ /pic/p>

  $content .= file_get_contents($file); /pic/p>

  }

  /pic/p>

  /pic/p>

  $new_file = fopen($destination_dir . $dest_file_name, "w" ); /pic/p>

  fwrite($new_file , $content); /pic/p>

  fclose($new_file);

  return ''; /pic/p>

  }else{

  /pic/p>

  return ''; /pic/p>

  }

  }

  并且,用法是這樣的:

  $files = array(

  '/pic/files/sample_js_file_1.js',

  '/pic/files/sample_js_file_2.js',

  '/pic/files/beautyquote_functions.js',

  '/pic/files/crop.js',

  '/pic/files/jquery.autosize.min.js',

  );

  echo combine_my_files($files, 'minified_files/', md5("my_mini_file").".js");

  3.查看你的電子郵件是否已讀

  當(dāng)發(fā)送電子郵件時(shí),你會希望知道你的郵件是否已讀。這里有一個(gè)非常有趣的代碼片段,它可以記錄閱讀你郵件的IP地址,以及實(shí)際的日期和時(shí)間。

  error_reporting(0);

  Header("Content-Type: image/jpeg");

  /pic/p>

  if (!empty($_SERVER['HTTP_CLIENT_IP']))

  {

  $ip=$_SERVER['HTTP_CLIENT_IP'];

  }

  elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))

  {

  $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];

  }

  else

  {

  $ip=$_SERVER['REMOTE_ADDR'];

  }

  /pic/p>

  $actual_time = time();

  $actual_day = date('Y.m.d', $actual_time);

  $actual_day_chart = date('d/m/y', $actual_time);

  $actual_hour = date('H:i:s', $actual_time);

  /pic/p>

  $browser = $_SERVER['HTTP_USER_AGENT'];

  /pic/p>

  $myFile = "log.txt";

  $fh = fopen($myFile, 'a+');

  $stringData = $actual_day . ' ' . $actual_hour . ' ' . $ip . ' ' . $browser . ' ' . " ";

  fwrite($fh, $stringData);

  fclose($fh);

  /pic/p>

  $newimage = ImageCreate(1,1);

  $grigio = ImageColorAllocate($newimage,255,255,255);

  ImageJPEG($newimage);

  ImageDestroy($newimage);

  ?>

  4.從網(wǎng)頁提取關(guān)鍵詞

  正如這小標(biāo)題所說的那樣:這個(gè)代碼片段能讓你輕易地從網(wǎng)頁中提取元關(guān)鍵詞。

  $meta = get_meta_tags('/pic/');

  $keywords = $meta['keywords'];

  /pic/p>

  $keywords = explode(',', $keywords );

  /pic/p>

  $keywords = array_map( 'trim', $keywords );

  /pic/p>

  $keywords = array_filter( $keywords );

  print_r( $keywords );

  5.查找頁面上的所有鏈接

  使用DOM,你可以輕松地抓取來網(wǎng)頁上的所有鏈接。這里有一個(gè)工作示例:

  $html = file_get_contents('/pic/p>

  $dom = new DOMDocument();

  @$dom->loadHTML($html);

  /pic/p>

  $xpath = new DOMXPath($dom);

  $hrefs = $xpath->evaluate("/html/body/pic/p>

  for ($i = 0; $i < $hrefs->length; $i++) {

  $href = $hrefs->item($i);

  $url = $href->getAttribute('href');

  echo $url.'

  ';

  }

  6.自動(dòng)轉(zhuǎn)換URL為可點(diǎn)擊的超鏈接

  在WordPress中,如果你想在字符串中自動(dòng)轉(zhuǎn)換所有的URL成可點(diǎn)擊的超鏈接,那么使用內(nèi)置函數(shù)make_clickable()可以讓你心想事成。如果你需要在WordPress之外這么做,那么你可以在wp-includes/formatting.php參考該函數(shù)的源代碼:

  function _make_url_clickable_cb($matches) {

  $ret = '';

  $url = $matches[2];

  if ( empty($url) )

  return $matches[0];

  /pic/p>

  if ( in_array(substr($url, -1), array('.', ',', ';', ':')) === true ) {

  $ret = substr($url, -1);

  $url = substr($url, 0, strlen($url)-1);

  }

  return $matches[1] . "$url" . $ret;

  }

  function _make_web_ftp_clickable_cb($matches) {

  $ret = '';

  $dest = $matches[2];

  $dest = '/pic/p>

  if ( empty($dest) )

  return $matches[0];

  /pic/p>

  if ( in_array(substr($dest, -1), array('.', ',', ';', ':')) === true ) {

  $ret = substr($dest, -1);

  $dest = substr($dest, 0, strlen($dest)-1);

  }

  return $matches[1] . "$dest" . $ret;

  }

  function _make_email_clickable_cb($matches) {

  $email = $matches[2] . '@' . $matches[3];

  return $matches[1] . "$email";

  }

  function make_clickable($ret) {

  $ret = ' ' . $ret;

  /pic/p>

  $ret = preg_replace_callback('#([s>])([w]+?:/pic/.-;:=,?@[]+]*)#is', '_make_url_clickable_cb', $ret);

  $ret = preg_replace_callback('#([s>])((www|ftp).[wx80-xff#$%&~/.-;:=,?@[]+]*)#is', '_make_web_ftp_clickable_cb', $ret);

  $ret = preg_replace_callback('#([s>])([.0-9a-z_+-]+)@(([0-9a-z-]+.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret);

  /pic/p>

  $ret = preg_replace("#(]+?>|>))]+?>([^>]+?)#i", "$1$3", $ret);

  $ret = trim($ret);

  return $ret;

  }

  7.在你的服務(wù)器上下載并保存遠(yuǎn)程圖像

  在遠(yuǎn)程服務(wù)器上下載一個(gè)圖像,并將其保存在自己的服務(wù)器上,在建立網(wǎng)站時(shí)很有用,而且這也很容易做到。下面的這兩行代碼就能為你辦到。

  $image = file_get_contents('/pic/image.jpg');

  file_put_contents('/images/image.jpg', $image); /pic/p>

  8.檢測瀏覽器語言

  如果你的網(wǎng)站使用多種語言,那么檢測瀏覽器語言,并將這種語言作為默認(rèn)語言會很有用。下面的代碼將返回客戶瀏覽器使用的語言。

  function get_client_language($availableLanguages, $default='en'){

  if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {

  $langs=explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);

  foreach ($langs as $value){

  $choice=substr($value,0,2);

  if(in_array($choice, $availableLanguages)){

  return $choice;

  }

  }

  }

  return $default;

  }

  9.全文顯示臉x書的粉絲數(shù)量

  如果你的網(wǎng)站或博客有一個(gè)臉x書的頁面,那么你可能想要顯示你有多少個(gè)粉絲。這個(gè)代碼片段可以幫助你獲取臉x書的粉絲數(shù)量。不要忘記在第二行添加你的頁面ID。頁面ID可以在地址/pic/yourpagename/info找到。

  $page_id = "302807633129400";

  $xml = @simplexml_load_file("/pic/restserver.php?method=臉x書.fql.query&query=SELECTfan_countFROMpageWHEREpage_id=".$page_id."") or die ("a lot");

  $fans = $xml->page->fan_count;

  echo $fans;

  ?>

【超級9個(gè)PHP代碼片段】相關(guān)文章:

實(shí)用的9個(gè)PHP代碼片段08-13

5個(gè)超級有用的php片段08-29

php分頁類代碼08-05

PHP代碼運(yùn)行流程07-15

PHP代碼優(yōu)化技巧10-01

PHP代碼如何規(guī)范02-13

php語言字典代碼02-15

PHP調(diào)用的C代碼08-27

PHP實(shí)用的代碼實(shí)例08-17