PHP Practical code fragment finishing _php Tips

Source: Internet
Author: User
Tags explode html tags json trim xpath

This article summarizes the PHP practical code fragment. Share to everyone for your reference, specific as follows:

A keyword extraction from the Web page

$meta = get_meta_tags (' http://www.jb51.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);

Two find all the links on the page

Using 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 of 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. ' <br/> ';
}

Three create data URI

The data URI can help in embedding images into HTML/CSS/JS, thereby saving HTTP requests. The following function can use $file to create a data URI.

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

Four download and save remote pictures to your server

When you're building a site, it's likely that you'll download images from a remote server to your own server, and the following code will help you implement this functionality.

$image = file_get_contents (' yun_qi_img/image.jpg ');
File_put_contents ('/images/image.jpg ', $image); Where to save the image

Five remove Microsoft Word HTML tags

When you use Microsoft Word, you create a lot of tag tags, such as font, span, style, class, and so on, which are useful in word, but when you paste text from Word into a Web page, there are a lot of 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 (<)? ( Font|span|del|ins) [^>]*> "," ", $html);
Then run another pass over the HTML (twice), removing unwanted attributes
$html = ereg_replace ("<" ([^>]*) (Clas S|lang|style|size|face) = ("[^"]* "| [^']*'| [^>]+] ([^>]*) > "," <\1> ", $html);
$html = Ereg_replace ("<" ([^>]*) (Class|lang|style|size|face) = ("[^"]* "|"] [^']*'| [^>]+] ([^>]*) > "," <\1> ", $html);
Return $html
}

Six detection Browser language

If your site is multilingual, the following code can help you detect the browser language, and it will return 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 request information to Local

Copy Code code as follows:
File_put_contents ('/tmp/all.log ', ' mapping '. Date ("M-d h:i:s"). " \ n ", file_append);

eight Excel converting dates to each other

If you go to get an Excel date (in the format: 2016-03-12), 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 convert to each other

1 JSON converts an array

$json = ' [{"id]:", "name": "DESCN": "44"}] '; The JSON-formatted array is converted into an array of PHP
$arr = (array) json_decode ($json);
Echo $arr [0]->id; Accessed in the form of an object (this is a case of converting an array without converting it into an object)

2 Array converted to JSON

$json _arr = Array (' WebName ' => ', ' WebSite ' => ');
$php _json = Json_encode ($json _arr); Converts the PHP array format into JSON-formatted data
echo $php _json;

More about PHP Interested readers can view the site topics: "PHP object-oriented Program Design Primer", "PHP basic Grammar Introductory Course", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Array" operation Skills Encyclopedia, " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips

I hope this article will help you with the PHP program design.

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.