Useful php code snippets,

Source: Internet
Author: User

Useful php code snippets,
1. Extract keywords from webpages

$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 );
2. Search for all links on the page
With DOM, you can capture links on any page, as shown in the following example.
3. Create a data URI 
Data URI can help embed images into HTML, CSS, and JS to save HTTP requests. The following functions can use $ file to create a data URI. Function data_uri ($ file, $ mime) {$ contents = file_get_contents ($ file); $ base64 = base64_encode ($ contents); echo "data: $ mime; base64, $ base64 ";}
4. Download and save remote images to your server
When you build a website, you may download images from the remote server and save them to your own server. The following code can help you implement this function. $ Image = file_get_contents ('HTTP: // www.php100.com/image.jpg'); file_put_contents ('/images/image.jpg', $ image); // Where to save the image
5. Remove Microsoft Word HTML tags
When you use Microsoft Word, you will create many tag tags, such as font, span, style, and class. These tags are very useful in Word, however, when you paste text from Word to a webpage, many useless labels will appear. The following practical functions can help you clear all the Word HTML tags. Function cleanHTML ($ html) {// Removes all FONT and SPAN tags, and all Class and Style attributes. /// Designed to get rid of non-standard Microsoft Word HTML tags. //// start by completely removing all unwanted tags $ html = ereg_replace ("<(/)? (Font | span | del | ins) [^>] *> "," ", $ html); // then run another pass over the html (twice ), removing unwanted attributes $ html = ereg_replace ("<([^>] *) (class | lang | style | size | face) = ("[^"] * "| '[^'] * '| [^>] +) ([^>] *)> ", "<\ 1>", $ html); $ html = ereg_replace ("<([^>] *) (class | lang | style | size | face) = ("[^"] * "| '[^'] * '| [^>] +) ([^>] *)> ", "<\ 1>", $ html); return $ html}
6. Check the browser Language
If your website is in multiple languages, the following code can help you detect the browser language. It will return the default language of the client browser. Function get_client_language ($ availableLanguages, $ default = 'en') {if (isset ($ _ SERVER ['HTTP _ ACCEPT_LANGUAGE ']) {$ langs = explode (',', $ _ SERVER ['HTTP _ ACCEPT_LANGUAGE ']); foreach ($ langs as $ value) {$ choice = substr ($ value,); if (in_array ($ choice, $ availableLanguages) {return $ choice ;}}return $ default ;}
7. Save the request information to the local device
file_put_contents('/tmp/all.log','mapping'.date("m-d H:i:s")."\n",FILE_APPEND);
8. excel exchange date
// If You Want To obtain an excel date in the format of, the obtained date is a number and can be recovered only after conversion.
Public function excelTime ($ date, $ time = false) {if (function_exists ('gregoriantojd ') {if (is_numeric ($ date) {$ jd = GregorianToJD (1, 1, 1970); $ gregorian = JDToGregorian ($ jd + intval ($ date)-25569); $ date = explode ('/', $ gregorian ); $ date_str = str_pad ($ date [2], 4, '0', STR_PAD_LEFT ). "-". str_pad ($ date [0], 2, '0', STR_PAD_LEFT ). "-". str_pad ($ date [1], 2, '0', STR_PAD_LEFT) . ($ Time? "00:00:00": ''); return $ date_str ;}} else {// $ date = $ date> 25568? $ Date + 1: 25569;/* There was a bug if Converting date before 1-1-1970 (tstamp 0) */$ ofs = (70*365 + 17 + 2) * 86400; $ date = date ("Y-m-d", ($ date * 86400)-$ ofs ). ($ time? "00:00:00": ''); return $ date ;}}
9 json and Data Conversion
1 json to array $ json = '[{"id": "22", "name": "33", "descn": "44"}]'; // convert an Array in json format to an Array in php $ arr = (Array) json_decode ($ json); echo $ arr [0]-> id; // access using an object (this is not converted to an array, but to an object)
2. Convert the array to json $ json_arr = array ('webname' => '11', 'website' => '11'); $ php_json = json_encode ($ json_arr ); // convert the php array format into json data echo $ php_json;

 

 

 

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.