Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the php utility code snippets?

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you what php practical code snippets are, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

The details are as follows:

First, extract keywords from the web page

$meta = get_meta_tags ('https://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)

Find all the links on the page

With DOM, you can crawl 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; $iTunes +) {$href = $hrefs- > item ($I); $url = $href- > getAttribute ('href'); echo $url.'';}

3. Create data URI

The data URI can help embed the image in the HTML/CSS/JS, thereby saving HTTP requests. The following function creates a data URI using $file.

Function data_uri ($file, $mime) {$contents=file_get_contents ($file); $base64=base64_encode ($contents); echo _ "data:$mime;base64,$base64";}

Download and save remote pictures to your server

When you are building a website, you are likely to download pictures from a remote server and save them to your own server. The following code will help you achieve 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 a lot of tags tag, such as font, span, style, class, etc., these tags are very useful in Word, but when you paste text from Word into a web page, there will be a lot of useless tags. The following practical functions can help you clear all 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 ("] * >", ", $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}

Six detect browser language

If your site is multilingual, the following code will help you detect the browser language, which 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;}

7. Save the request information locally

The copy code is as follows:

File_put_contents ('/ tmp/all.log','mapping'.date ("Mmurd H:i:s"). "\ n", FILE_APPEND)

8 excel conversion date

/ / if you go to get an excel date (format: 2016-03-12), you get a number, and you need to convert to recover 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, '0mm, STR_PAD_LEFT). "-". Str_pad ($date [0], 2,'0, STR_PAD_LEFT). "-". Str_pad ($date [1], 2, '0mm, 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 + 176400) * 86400; $date= date ("Y-m-d", ($date * 86400)-$ofs). ($time? "00:00:00":'); return $date;}}

9. Conversion between json and data

1 json converted to an array

$json ='[{"id": "22", "name": "33", "descn": "44"}]'; / / an array in json format is converted into an array of php $arr = (Array) json_decode ($json); echo $arr [0]-> id; / / is accessed as an object (this is when an array is not converted to an array, but to an object

Convert 2 array to json

$json_arr = array ('WebName'= >' 1113); $php_json = json_encode ($json_arr); / / convert the php array format to json format data echo $php_json; is all the contents of this article "what are the practical code snippets of php?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report