9 Useful php code snippets, _ PHP Tutorial

Source: Internet
Author: User
9 Useful php code snippets ,. We will share nine useful php code snippets for your reference, the specific code is as follows: 1. extract the keyword $ metaget_meta_tags from the webpage (9 useful php code snippets,

Useful php code snippets for your reference. the specific code is as follows:

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.

$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.'
';}

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,0,2);       if(in_array($choice, $availableLanguages)){          return $choice;       }     }   }    return $default;}

7. Save the request information to the local device.
The code is as follows: file_put_contents ('/tmp/all. log', 'mapping'. date ("m-d H: I: s"). "\ n", FILE_APPEND );
8. date of mutual conversion in excel

// If you want to obtain an excel date in the format of, the obtained date is a number and must be converted to restore the 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_LEF T). "-". 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 ;}}

IX. conversion between json and data

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;

The above is all the content of this article, hoping to help you learn.

Articles you may be interested in:
  • Seven Super Practical PHP code snippets
  • 10 Practical PHP code snippets
  • Handling code snippets with common strings in php
  • Super practical seven PHP code snippets
  • PHP security detection code snippets (share)
  • 19 ultra-practical PHP code snippets
  • Share nine classic PHP code snippets
  • Six ultra-practical PHP code snippets
  • Ten more useful PHP code snippets
  • 46 useful PHP code snippets

Examples, useful php code snippets, for your reference. the specific code is as follows: 1. extract the keyword $ meta = get_meta_tags ('...

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.