Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Sharing the method of writing practical code snippets in php

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "sharing php practical code snippet writing methods", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "sharing php practical code snippet writing method"!

24. Create a CSV file from PHP data

Function generateCsv ($data, $delimiter =',', $enclosure ='") {$handle = fopen ('php://temp', 'ritual'); foreach ($data as $line) {fputcsv ($handle, $line, $delimiter, $enclosure);} rewind ($handle); while (! feof ($handle)) {$contents. = fread ($handle, 8192);} fclose ($handle); return $contents;}

Syntax:

twenty-nine。 Number of retweets

Use this PHP snippet to check the number of retweets on your page URL

Function tweetCount ($url) {$content = file_get_contents ("http://api.tweetmeme.com/url_info?url=".$url); $element = new SimpleXmlElement ($content); $retweets = $element- > story- > url_count; if ($retweets) {return $retweets;} else {return 0;}}

Syntax:

thirty。 Calculate the difference between two dates

thirty-one。 Delete folder contents

Function Delete ($path) {if (is_dir ($path) = true) {$files = array_diff (scandir ($path), array ('.','..')); foreach ($files as $file) {Delete (realpath ($path). '/'. $file);} return rmdir ($path);} else if (is_file ($path) = true) {return unlink ($path);} return false;}

Syntax:

thirty-two。 Search for and highlight keywords in strings

Function highlighter_text ($text, $words) {$split_words = explode ("", $words); foreach ($split_words as $word) {$color = "# 4285F4"; $text = preg_replace ("| ($word) | Ui", "$1", $text);} return $text;}

Syntax:

thirty-three。 Write to a file

thirty-four。 Download pictures according to URL

Function imagefromURL ($image,$rename) {$ch = curl_init ($image); curl_setopt ($ch, CURLOPT_HEADER, 0); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_BINARYTRANSFER,1); $rawdata=curl_exec ($ch); curl_close ($ch); $fp = fopen ("$rename",'w'); fwrite ($fp, $rawdata); fclose ($fp);}

Syntax:

thirty-five。 Check whether URL is valid

Function isvalidURL ($url) {$check = 0 check; if (filter_var ($url, FILTER_VALIDATE_URL)! = = false) {$check = 1;} return $check;}

Syntax:

thirty-six。 Generate a QR code

Function qr_code ($data, $type = "TXT", $size = '150 million, $ec='L', $margin='0') {$types = array ("URL" =-> "http://"," TEL "= >" TEL: "," TXT "= >", "EMAIL" = > "MAILTO:"); if (! in_array ($type,array ("URL", "TEL", "TXT", "EMAIL")) {$type = "TXT") } if (! preg_match ('/ ^'. $types [$type].'/', $data) {$data = str_replace ("\\", ", $types [$type]). $data;} $ch = curl_init (); $data = urlencode ($data); curl_setopt ($ch, CURLOPT_URL, 'http://chart.apis.google.com/chart'); curl_setopt ($ch, CURLOPT_POST, true) Curl_setopt ($ch, CURLOPT_POSTFIELDS, 'chs='.$size.'x'.$size.'&cht=qr&chld='.$ec.' |'. $margin.'&chl='.$data); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt ($ch, CURLOPT_HEADER, false); curl_setopt ($ch, CURLOPT_TIMEOUT, 30); $response = curl_exec ($ch); curl_close ($ch); return $response;}

Syntax:

thirty-seven。 Calculate the distance between two map coordinates

Function getDistanceBetweenPointsNew ($latitude1, $longitude1, $latitude2, $longitude2) {$theta = $longitude1-$longitude2; $miles = (sin (deg2rad ($latitude1)) * sin (deg2rad ($latitude2) + (cos (deg2rad ($latitude1)) * cos (deg2rad ($latitude2)) * cos (deg2rad ($theta); $miles = acos ($miles); $miles = rad2deg ($miles); $miles = $miles * 60 * 1.1515; $feet = $miles * 5280; $yards = $feet / 3; $feet = $kilometers * 1.609344; $kilometers = $kilometers * 1000 Return compact ('miles','feet','yards','kilometers','meters');}

Syntax:

thirty-eight。 Get all the Tweets of a specific topic tag

Function getTweets ($hash_tag) {$url = 'http://search.twitter.com/search.atom?q='.urlencode($hash_tag); echo "

Connecting to $url...

"; $ch = curl_init ($url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE); $xml = curl_exec ($ch); curl_close ($ch); / / If you want to see the response from Twitter, uncomment this next part out: / / echo"

Response:

"; / echo".htmlspecialchars ($xml)."; $affected = 0; $twelement = new SimpleXMLElement ($xml); foreach ($twelement- > entry as $entry) {$text = trim ($entry- > title); $author = trim ($entry- > author- > name); $time = strtotime ($entry- > published); $id = $entry- > id; echo "

Tweet from ". $author.": ". $text." Posted ".date ('n/j/y GRV I am responsible for the last month)."

";} return true;}

thirty-nine。 Add th,st,nd or rd as the suffix of the number

Friday the 13thfunction ordinal ($cdnl) {$test_c = abs ($cdnl)% 10; $ext = ((abs ($cdnl) 0)

< 21 && abs($cdnl) 0 >

4)? 'th': ($test_c < 4)? ($test_c < 3)? ($test_c < 2)? ($test_c < 1)? 'th':' st': 'nd':' rd': 'th'); return $cdnl.$ext;}

Syntax:

forty。 Limit the speed of file download

forty-one。 Convert text into pictures

forty-two。 Get the size of the remote file

Function remote_filesize ($url, $user = "", $pw = "") {ob_start (); $ch = curl_init ($url); curl_setopt ($ch, CURLOPT_HEADER, 1); curl_setopt ($ch, CURLOPT_NOBODY, 1); if (! empty ($user) & &! empty ($pw)) {$headers = array ('Authorization: Basic'. Base64_encode ("$user:$pw"); curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);} $ok = curl_exec ($ch); curl_close ($ch); $head = ob_get_contents (); ob_end_clean (); $regex ='/ Content-Length:\ s ([0-9]. +?)\ s Universe; $count = preg_match ($regex, $head, $matches); return isset ($matches [1])? $matches [1]: "unknown";}

Grammar

forty-three。 Using imagebrick to convert pdf to Image

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report