php语言

5个超级有用的php片段

时间:2024-06-05 22:24:51 php语言 我要投稿
  • 相关推荐

5个超级有用的php片段

  尽管PHP经常被人诟病,被人贬低,被人当玩笑开,事实证明,PHP是全世界网站开发中使用率最高的编程语言。PHP最大的缺点是太简单,语法不严谨,框架体系很弱,但这也是它最大的优点,一个有点编程背景的普通人,只需要学习PHP半天时间,就可以上手开始开发web应用了

  一、黑名单过滤

  function is_spam($text, $file, $split = ':', $regex = false){

  $handle = fopen($file, 'rb');

  $contents = fread($handle, filesize($file));

  fclose($handle);

  $lines = explode("n", $contents);

  $arr = array();

  foreach($lines as $line){

  list($word, $count) = explode($split, $line);

  if($regex)

  $arr[$word] = $count;

  else

  $arr[preg_quote($word)] = $count;

  }

  preg_match_all("~".implode('|', array_keys($arr))."~", $text, $matches);

  $temp = array();

  foreach($matches[0] as $match){

  if(!in_array($match, $temp)){

  $temp[$match] = $temp[$match] + 1;

  if($temp[$match] >= $arr[$word])

  return true;

  }

  }

  return false;

  }

  $file = 'spam.txt';

  $str = 'This string has cat, dog word';

  if(is_spam($str, $file))

  echo 'this is spam';

  else

  echo 'this is not spam';

  ab:3

  dog:3

  cat:2

  monkey:2

  二、随机颜色生成器

  复制代码 代码如下:

  function randomColor() {

  $str = '#';

  for($i = 0 ; $i < 6 ; $i++) {

  $randNum = rand(0 , 15);

  switch ($randNum) {

  case 10: $randNum = 'A'; break;

  case 11: $randNum = 'B'; break;

  case 12: $randNum = 'C'; break;

  case 13: $randNum = 'D'; break;

  case 14: $randNum = 'E'; break;

  case 15: $randNum = 'F'; break;

  }

  $str .= $randNum;

  }

  return $str;

  }

  $color = randomColor();

  三、从网络下载文件

  复制代码 代码如下:

  set_time_limit(0);

  // Supports all file types

  // URL Here:

  $url = 'http://somsite.com/some_video.flv';

  $pi = pathinfo($url);

  $ext = $pi['extension'];

  $name = $pi['filename'];

  // create a new cURL resource

  $ch = curl_init();

  // set URL and other appropriate options

  curl_setopt($ch, CURLOPT_URL, $url);

  curl_setopt($ch, CURLOPT_HEADER, false);

  curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);

  curl_setopt($ch, CURLOPT_AUTOREFERER, true);

  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

  // grab URL and pass it to the browser

  $opt = curl_exec($ch);

  // close cURL resource, and free up system resources

  curl_close($ch);

  $saveFile = $name.'.'.$ext;

  if(preg_match("/[^0-9a-z._-]/i", $saveFile))

  $saveFile = md5(microtime(true)).'.'.$ext;

  $handle = fopen($saveFile, 'wb');

  fwrite($handle, $opt);

  fclose($handle);

  四、Alexa/Google Page Rank

  function page_rank($page, $type = 'alexa'){

  switch($type){

  case 'alexa':

  $url = 'http://alexa.com/siteinfo/';

  $handle = fopen($url.$page, 'r');

  break;

  case 'google':

  $url = 'http://google.com/search?client=navclient-auto&ch=6-1484155081&features=Rank&q=info:';

  $handle = fopen($url.'http://'.$page, 'r');

  break;

  }

  $content = stream_get_contents($handle);

  fclose($handle);

  $content = preg_replace("~(n|t|ss+)~",'', $content);

  switch($type){

  case 'alexa':

  if(preg_match('~

  (.+?)

  ~im',$content,$matches)){

  return $matches[2];

  }else{

  return FALSE;

  }

  break;

  case 'google':

  $rank = explode(':',$content);

  if($rank[2] != '')

  return $rank[2];

  else

  return FALSE;

  break;

  default:

  return FALSE;

  break;

  }

  }

  // Alexa Page Rank:

  echo 'Alexa Rank: '.page_rank('techug.com');

  echo '

  ';

  // Google Page Rank

  echo 'Google Rank: '.page_rank('techug.com', 'google');

  五.从网页提取关键词

  正如这小标题所说的那样:这个代码片段能让你轻易地从网页中提取元关键词。

  $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 );

【5个超级有用的php片段】相关文章:

超级实用的9个PHP代码片段07-25

超级有用的PS技巧09-16

对初学者非常有用的PHP技巧07-30

php与php MySQL之间的关系10-03

什么是PHP PHP与ASP比较09-18

php正则去掉php注释07-30

PHP中php://input和$-POST的区别08-26

PHP试题07-04

PHP是做什么的 如何学习PHP06-18

php的发展历程08-23