11 small tips code snippets for daily php use: www. phpzag. com11-useful-code-snippets-for-php-developers simple summary 1 with php built-in csv function can be very convenient to generate CSV, & nbsp; functiongenerateCsv ($ data, $ delimiter ,, $ enc 11 small tips code snippets for daily php use
Http://www.phpzag.com/11-useful-code-snippets-for-php-developers/
Summary
1. you can use the built-in csv function of php to easily generate CSV files,
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;}
Usage:
$data = array( array(1, 2, 4), array('test string', 'test, literal, comma', 'test literal "quotes"'), );echo generateCsv($data);
2. invalid input filtering
Usage:
《script》 It’s a good day!”; $good_string = sanitize_input_data($bad_string); //OUTPUT:: Hi! It\’s a good day! ?>
3 unzip file
Php has built-in unzip
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->extractTo($destination);// close archive$zip->close();echo 'Archive extracted to directory';}
4. use get_meta_tags to obtain the meta keyword of a webpage.
$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. determine whether the server is https
if ($_SERVER['HTTPS'] != "on") { echo "This is not HTTPS";}else{echo "This is HTTPS";}
6
Display all source code of a 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 using datauri makes it easy to convert images into base64. the code is as follows:
function data_uri($file, $mime) { $contents=file_get_contents($file); $base64=base64_encode($contents); echo "data:$mime;base64,$base64";}
8. obtain all connections to a webpage
$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.'';}
9
Make the questions on the page more friendly for SEO,
function make_seo_name($title) {return preg_replace('/[^a-z0-9_-]/i', '', strtolower(str_replace(' ', '-', trim($title))));}
For example, there is a title:
"This foo's bar is rockin 'Cool! ";
The output is:
This-foos-bar-is-rockin-cool
10
Download images from a webpage and put them on your server.
$image = file_get_contents('http://www.url.com/image.jpg');file_put_contents('/images/image.jpg', $image);
The 11 page shows the number of facebook friends (not useful for Chinese people)
function fb_fan_count($facebook_name){ $data = json_decode(file_get_contents("https://graph.facebook.com/".$facebook_name)); echo $data->likes;}