Several Practical PHP code snippets [self-backup]
This is indeed a simple function. a. csv file is generated from a PHP Group. This function uses the built-in fputcsv PHP function to generate comma-separated files (. CSV ). This function has three parameters: Data. the delimiter and CSV enclosure are double quotation marks by default.
2.
[Code]Use PHP to clear malicious code from database input
This is a useful PHP function that clears all input data and deletes the chance of code injection. 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 ;}
3.
[Code]Decompress the File Unzip using PHP
This is a very convenient PHP function. Decompress the zip file. It has two parameters: the first is the path of the compressed file and the second is the path of the target file. Function unzip_file ($ file, $ destination) {// create object $ zip = new ZipArchive (); // open archiveif ($ zip-> open ($ file )! = TRUE) {die ('could not open Archive');} // extract contents to destination directory $ zip-> extracation ($ destination ); // close archive $ zip-> close (); echo 'archiveextracted to directory ';}
4.
[Code]Keywords extracted from webpages
It is really a very useful code snippet to extract meta keywords from any web page. $ Meta = get_meta_tags ('http: // www.emoticode.net/'); $ keywords = $ meta ['keyword']; // Split keywords $ keywords = explode (',', $ keywords); // Trim them $ keywords = array_map ('trim', $ keywords); // Remove empty values $ keywords = array_filter ($ keywords ); print_r ($ keywords );
5.
[Code]Check whether the server is HTTPS
This PHP code snippet can read information about SSL activation (HTTPS) on your server. If ($ _ SERVER ['https']! = "On") {echo "This is not HTTPS";} else {echo "This is HTTPS ";}
6.
[Code]Display source code on any webpage
$lines = file('http://google.com/');foreach ($lines as $line_num => $line) { // loop thru each line and prepend line numbersecho "Line #{$line_num} : " . htmlspecialchars($line) . "\n";}
7.
[Code]URI of the created data
As we know, data URIs can embed images into HTML, CSS, and JS to save HTTP requests. This is a very practical PHP code snippet to create a data URI. Function data_uri ($ file, $ mime) {$ contents = file_get_contents ($ file); $ base64 = base64_encode ($ contents); echo "data: $ mime; base64, $ base64 ";}
8.
[Code]Retrieve all links on a page
Get all the links in a page $ html = file_get_contents ('http: // blogtail907.org '); $ dom = new DOMDocument (); @ $ dom-> loadHTML ($ html ); // grab all the on the page $ xpath = new DOMXPath ($ dom); $ hrefs = $ xpath-> evaluate ("/html/body // "); for ($ I = 0; $ I <$ hrefs-> length; $ I ++) {$ href = $ hrefs-> item ($ I ); $ url = $ href-> getAttribute ('href '); echo $ url. '';}
9.
[Code]Make the webpage title more friendly to search engines
function make_seo_name($title) {return preg_replace('/[^a-z0-9_-]/i', '', strtolower(str_replace(' ', '-', trim($title))));}
10.
[Code]Use PHP to download and save remote images on your server.
$image = file_get_contents('http://blog.0907.org/wp-content/uploads/2014/03/xunlei.jpg');file_put_contents('/images/image.jpg', $image); //save the image on your server