PHP: a summary of some practical user-defined functions and a summary of php user-defined functions. PHP: a summary of some practical user-defined functions, and a summary of php user-defined functions. I will share it with you for your reference. The details are as follows: I have recently read some practical user-defined functions (UDFs) and PHP user-defined functions (UDFs ).
This example summarizes several practical user-defined functions in PHP. We will share this with you for your reference. The details are as follows:
I recently read the code and found that the following are some useful functions.
1. obtain the client IP address
function getOnlineIp() { $strOnlineIp = ""; if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) { $onlineip = getenv('HTTP_CLIENT_IP'); } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) { $onlineip = getenv('HTTP_X_FORWARDED_FOR'); } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) { $onlineip = getenv('REMOTE_ADDR'); } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) { $onlineip = $_SERVER['REMOTE_ADDR']; } preg_match("/[\d\.]{7,15}/", $onlineip, $onlineipmatches); $strOnlineIp = $onlineipmatches[0] ? $onlineipmatches[0] : 'unknown'; return $strOnlineIp;}
Here, if you use a multi-level proxy, the real IP address cannot be obtained.
2. string truncation, supporting Chinese characters
Function getStrTruncate ($ string, $ length = 80, $ etc = '') {if ($ length = 0) return''; mb_internal_encoding ("UTF-8 "); $ string = str_replace ("\ n", "", $ string); $ strlen = mb_strwidth ($ string); if ($ strlen> $ length) {$ etclen = mb_strwidth ($ etc); $ length = $ length-$ etclen; $ str = ''; $ n = 0; for ($ I = 0; $ I <$ length; $ I ++) {$ c = mb_substr ($ string, $ I, 1); $ n + = mb_strwidth ($ c ); if ($ n> $ length) {break;} $ str. = $ c;} return $ str. $ etc;} else {return $ string;} echo getStrTruncate
3. how long ago are time functions commonly used in forums and blogs?
Function timeFromNow ($ dateline) {if (emptyempty ($ dateline) return false; $ seconds = time ()-$ dateline; if ($ seconds <60) {return "1 minute ago";} elseif ($ seconds <3600) {return floor ($ seconds/60 ). "minutes ago";} elseif ($ seconds <24*3600) {return floor ($ seconds/3600 ). "hours ago";} elseif ($ seconds <48*3600) {return date ("yesterday H: I", $ dateline ). "";} else {return date ('Y-m-D', $ dateline) ;}} echo timeFromNow (strtotime ("14:15:13"); // Yesterday 14: 15 echo timeFromNow (strtotime ("14:15:13"); // Before 1
4. filter some tags
Function delTags ($ str) {$ farr = array ("/<(\/?) (Script | I? Frame | style | html | body | title | link | meta | form | input | embed | object | textarea | \? | \ %) ([^>] *?)> /IsU ","/(<[^>] *) on [a-zA-Z] + \ s * = ([^>] *>)/isU "); $ tarr = array ("", ""); $ str = preg_replace ($ farr, $ tarr, $ str); return $ str ;} $ str = "asdfasdfsd === script alert (1111) script"; echo delTags ($ str); // result: asdfasdfsd === alert (1111) echo strip_tags ($ str); // Result: sdfasdfsd === alert (1111)
It is enough to filter all tags in strip_tags.