In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what are the practical PHP code snippets". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what practical PHP code snippets".
Check to see if the email has been read
When you are sending an email, you may want to know if the email has been read by the other party. Here is a very interesting code snippet that shows the actual date and time the IP address record was read.
The copy code is as follows:
Second, extract keywords from web pages
A great code snippet can easily extract keywords from a web page.
The copy code is as follows:
$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
With DOM, you can easily crawl links from any page. The code example is as follows:
The copy code is as follows:
$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; $iTunes +) {
$href = $hrefs- > item ($I)
$url = $href- > getAttribute ('href')
Echo $url.''
}
Automatically convert URL and jump to hyperlink
In WordPress, if you want to automatically convert URL and jump to the hyperlink page, you can use the built-in function make_clickable () to do this. If you want to operate the program outside of WordPress, you can refer to the wp-includes/formatting.php source code.
The copy code is as follows:
Function _ make_url_clickable_cb ($matches) {
$ret =''
$url = $matches [2]
If (empty ($url))
Return $matches [0]
/ / removed trailing [.,;:] from URL
If (in_array (substr ($url,-1), array ('.,';',':)) = = true) {
$ret = substr ($url,-1)
$url = substr ($url, 0, strlen ($url)-1)
}
Return $matches [1]. "$url". $ret
}
Function _ make_web_ftp_clickable_cb ($matches) {
$ret =''
$dest = $matches [2]
$dest = 'http://'. $dest
If (empty ($dest))
Return $matches [0]
/ / removed trailing [,;:] from URL
If (in_array (substr ($dest,-1), array ('.,';',':)) = = true) {
$ret = substr ($dest,-1)
$dest = substr ($dest, 0, strlen ($dest)-1)
}
Return $matches [1]. "$dest". $ret
}
Function _ make_email_clickable_cb ($matches) {
$email = $matches [2]. @. $matches [3]
Return $matches [1]. "$email"
}
Function make_clickable ($ret) {
$ret ='. $ret
/ / in testing, using arrays here was found to be faster
$ret = preg_replace_callback ('# ([\ s >]) ([\ w] +?: / / [\ w\\ x80 -\\ xff\ # $% & ~ /.\ -;: =,? @\ [\] +] *) # is','_ make_url_clickable_cb', $ret)
$ret = preg_replace_callback ('# ([\ s >]) ((www | ftp)\. [\ w\\ x80 -\\ xff\ # $% & ~ /.\ -;: =,? @\ [\] +] *) # is','_ make_web_ftp_clickable_cb', $ret)
$ret = preg_replace_callback ('# ([\ s >])) ([.0-9aMuz colors -] +) @ (([0-9aMuz -] +\.) + [0-9a-z] {2,}) # iTunes,'_ make_email_clickable_cb', $ret)
/ / this one is not in an array because we need it to run last, for cleanup of accidental links within links
$ret = preg_replace ("# (] +? > | >)) +? > ([^ >] +?) # I", "$1 $3", $ret)
$ret = trim ($ret)
Return $ret
}
5. Create data URL
The data URL can be embedded directly into the HTML/CSS/JS to save a large number of HTTP requests. The following code makes it easy to create a data URL using $file.
The copy code is as follows:
Function data_uri ($file, $mime) {
$contents=file_get_contents ($file)
$base64=base64_encode ($contents)
Echo _ "data:$mime;base64,$base64"
}
Download from the server & save a remote picture
When you are building a website, it is often used to download a picture from a remote server and save it on your own server. The code is as follows:
The copy code is as follows:
$image = file_get_contents ('https://cache.yisu.com/upload/information/20201209/266/42288.jpg');
File_put_contents ('/ images/image.jpg', $image); / / Where to save the image
7. Remove Remove Microsoft Word HTML Tag
When you use Microsoft Word, you will create a lot of Tag, such as font,span,style,class, etc. These tags are very useful for Word itself, but when you paste from Word to a web page, you will find a lot of useless Tag. Therefore, the following code will help you remove all useless Word HTML Tag.
The copy code is as follows:
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 ("] * >", ", $html)
/ / then run another pass over the html (twice), removing unwanted attributes
$html = ereg_replace ("] *) (class | lang | style | size | face) = (" [^ "] *" |'[^'] *'| [^ >] +) ([^ >] *) > ",", $html)
$html = ereg_replace ("] *) (class | lang | style | size | face) = (" [^ "] *" |'[^'] *'| [^ >] +) ([^ >] *) > ",", $html)
Return $html
}
[code]
8. Check the browser language
If you have multiple languages on your site, you can use this code as the default language to detect the browser language. This code returns the initial language used by the browser client.
[code]
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
}
9. Display the number of Facebook fans
If you have an inline Facebook page on your website or blog, you may want to know how many fans you have. This code will help you check the number of Facebook fans. Remember, don't forget to add this code to the second line of ID on your page.
The copy code is as follows:
Thank you for reading, the above is the content of "what practical PHP code snippets", after the study of this article, I believe you have a deeper understanding of what practical PHP code snippets are available, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.