Summary of some of the functions commonly used in PHP (induction)

Source: Internet
Author: User
Tags explode
This article introduces to you about some of the common functions of PHP Summary (induction), there is a certain reference value, the need for friends can refer to, I hope to help you.

1. Get the Web site http or https?

$http _type = ((Isset ($_server[' https ") && $_server[' https '] = = ' on ') | | (Isset ($_server[' Http_x_forwarded_proto ') && $_server[' http_x_forwarded_proto '] = = ' https ')) ? ' https://': '/HTTP ';

2. Delete empty data from the array

function Where_data ($data) {    foreach ($data as $k = = $v) {        if (empty ($v) && $v! = ' 0 ') {            unset ($data [ $k]);        }    }    return $data;}

3. Capturing part of the rich text

/** * Capture text in Rich text as part of * @param $content * @return string */function html_substr_content ($content, $length =100) {    $ Content = Htmlspecialchars_decode ($content);      Convert some of the predefined HTML entities to characters    $content = Str_replace ("", "", $content);     Replace the space with an empty    $content = Strip_tags ($content);                 The function strips HTML, XML, and PHP tags from the string to get plain text content    $con = mb_substr ($content, 0, $length, "Utf-8");   Returns the character of the first 100 string length in the string return    $con;}

First, blacklist filter

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); $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 have cat, dog word '; if (Is_spam ($str, $file)) echo ' This is spam '; Elseecho ' This is no T spam '; ab:3dog:3cat:2monkey:2

Second, random color generator

function Randomcolor () {  $str = ' # ';  for ($i = 0; $i < 6; $i + +) {    $randNum = rand (0);    Switch ($randNum) {case      ten: $randNum = ' A ';      Case one: $randNum = ' B '; break;      Case: $randNum = ' C '; break;      Case: $randNum = ' D '; break;      Case: $randNum = ' E '; break;      Case: $randNum = ' F '; break;    }    $str. = $randNum;  }  return $STR;} $color = Randomcolor ();

Third, download the file from the Internet

Set_time_limit (0);//Supports all file types//urls here: $url = ' http://php.cn/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 Optionscurl_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 resourcescurl_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);

Iv. Mandatory download of files

$filename = $_get[' file ']; Get the Fileid from the url//Query the file Id$query = sprintf ("select * from tableName WHERE ID = '%s '", Mysql_real_esc Ape_string ($filename)); $sql = mysql_query ($query), if (Mysql_num_rows ($sql) > 0) {$row = Mysql_fetch_array ($sql);// Set some headersheader ("Pragma:public"); Header ("expires:0"); Header ("Cache-control:must-revalidate, post-check=0, Pre-check=0 "); Header (" Content-type:application/force-download "); Header (" Content-type:application/octet-stream ") ); Header ("Content-type:application/download"); header ("Content-disposition:attachment; Filename= ". basename ($row [' FileName '])."; "); Header ("Content-transfer-encoding:binary"), Header ("Content-length:". FileSize ($row [' FileName ']); @readfile ($row [' FileName ']); Exit (0);} Else{header ("Location:/"); exit;}

V. Interception of pictures

$filename = "test.jpg"; list ($w, $h, $type, $attr) = getimagesize ($filename); $src _im = Imagecreatefromjpeg ($filename); $ src_x = ' 0 '; Begin x$src_y = ' 0 '; Begin y$src_w = ' 100 '; Width$src_h = ' 100 '; height$dst_x = ' 0 '; Destination x$dst_y = ' 0 '; Destination Y$dst_im = Imagecreatetruecolor ($src _w, $src _h); $white = Imagecolorallocate ($dst _im, 255, 255, 255); Imagefill ($dst _im, 0, 0, $white), Imagecopy ($dst _im, $src _im, $dst _x, $dst _y, $src _x, $src _y, $src _w, $src _h); Header (" Content-type:image/png "); Imagepng ($dst _im); Imagedestroy ($dst _im);

Vi. checking the website for downtime

function Visit ($url) {    $agent = "mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0) "; $ch =curl_init (); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, curlopt_useragent, $agent); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_verbose,false); curl_setopt ($ch, CURLOPT_ TIMEOUT, 5); curl_setopt ($ch, Curlopt_ssl_verifypeer, FALSE); curl_setopt ($ch, curlopt_sslversion,3); curl_setopt ($ch , Curlopt_ssl_verifyhost, FALSE); $page =curl_exec ($ch);//echo curl_error ($ch); $httpcode = Curl_getinfo ($ch, Curlinfo_ Http_code); Curl_close ($ch); if ($httpcode >=200 && $httpcode <300) return true;else return false;} if (Visit ("http://www.google.com")) echo "Website OK". " n "; Elseecho" Website down ";

Related articles recommended:

What is a PHP reference variable? How references in PHP are implemented

What is the difference between PHP7 and php5? The contrast between PHP5 and PHP7

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.