More useful PHP code snippets, share for everyone to refer to, the specific code as follows
First, extract 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 ($keywords);
Print_r ($keywords);
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/> ';
}
Third, create the 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
}
Vi. Detecting 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;
}
Vii. 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 convert date 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 conversion
1 JSON converts an array
of $json = ' [{] id ': ', ' name ': '//json ', ' descn ': ' The ' is '] '; the array in the format of the form is converted to an arrays 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;
The above is the entire content of this article, I hope to help you learn.