9 More useful PHP code snippets, _php tutorial

Source: Internet
Author: User
Tags explode

9 more useful snippets of PHP code,


More useful PHP code snippets, share to everyone for your reference, the specific code is as follows

First, extract the keywords from the Web page

$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 ($k Eywords);p Rint_r ($keywords);

Second, find all links on the page
Using the DOM, you can crawl links on any page, such as below.

$html = file_get_contents (' http://www.example.com '); $dom = new DOMDocument (); @ $dom->loadhtml ($html); Grab all 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 .'
';}

Third, create the data URI
Data URIs can help embed images into HTML/CSS/JS, saving HTTP requests. The following function can create a data URI using $file.

function Data_uri ($file, $mime) {  $contents =file_get_contents ($file);  $base 64=base64_encode ($contents);  echo "Data: $mime; base64, $base 64";}

Iv. downloading and saving remote images to your server

When you are building a website, you are likely to download images from a remote server to your own server, and the following code will help you implement this feature.

$image = file_get_contents (' http://www.php100.com/image.jpg '); file_put_contents ('/images/image.jpg ', $image);  Where to save the image

V. Remove Microsoft Word HTML tags
When you use Microsoft Word, you create many tag tags, such as font, span, style, class, and so on, that are useful in word, but when you paste text from Word onto a Web page, there are many useless tags. The following useful 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 ("&L t; (/)? (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}

Vi. Detection of Browser language
If your site is multilingual, the following code can help you detect the browser language, which returns to 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;}

Seven, save the request information to the local
Copy the Code code as follows: File_put_contents ('/tmp/all.log ', ' mapping '. Date ("M-d h:i:s"). " \ n ", file_append);
Eight, Excel convert dates to each other

If you go to get an Excel date (in the format: 2016-03-12), then you get a number that needs to be converted to restore 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 = (* 365 + 17+2) * 86400;        $date = Date ("Y-m-d", ($date * 86400)-$ofs). ($time? "00:00:00": ");        return $date;      }  }

Nine, JSON and data conversion

1 JSON conversion to an array of $json = ' [{' id ': ' "," "Name": "", "DESCN": "44"}] '; The JSON-formatted array is converted to an array of PHP $arr = (array) json_decode ($json); Echo $arr [0]->id; Accessed as an object (this is the case of converting an array to an object
The 2 array is converted to Json$json_arr = Array (' WebName ' = ' = ' one ', ' WebSite ' = ' one '); $php _json = Json_encode ($json _arr); Convert PHP array format to JSON-formatted data echo $php _json;

The above is the whole content of this article, I hope that everyone's study has helped.

Articles you may be interested in:

    • 7 super-useful PHP code Snippets
    • 10 useful snippets of PHP code
    • Common string processing code snippet collation in PHP
    • Super-Functional 7 PHP snippet sharing
    • PHP Security Detection Code snippet (share)
    • 19 Super-useful PHP code Snippets
    • 9 Classic PHP Code snippets to share
    • 6 super-useful PHP code Snippets
    • Another 10 super-useful PHP code Snippets
    • 46 very useful snippets of PHP code

http://www.bkjia.com/PHPjc/1110069.html www.bkjia.com true http://www.bkjia.com/PHPjc/1110069.html techarticle 9 more practical PHP code snippets, more useful PHP code snippets, share to everyone for reference, specific code as follows, extract keywords from the Web page $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.