This article mainly introduces several practical functions of PHP, combined with examples to summarize and analyze the PHP network operation, string manipulation, time operation and the use of regular expressions and other skills, the need for friends can refer to the next
Recently looking at the code, found that the following are some of the more useful functions.
1, take the client IP
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 multi-level proxy users, the real IP is not available.
2, string interception, support Chinese
function Getstrtruncate ($string, $length = n, $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 ("blog", 7); UTF8 a Chinese character corresponds to two characters
3, how long ago Time function, forum, blog commonly used
function Timefromnow ($dateline) { if (Emptyempty ($dateline)) return false; $seconds = Time ()-$dateline; if ($seconds <) { return "1 minutes 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 ("2012-07-07 14:15:13")); Yesterday 14:15echo Timefromnow (Strtotime ("2012-07-08 14:15:13")); 1 small Front
4, filter part of the label
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 = "<a href= ' # ' >asdfasdfsd</a>====<script>alert (1111) </script>"; Echo deltags ($STR); Results: <a href= ' # ' >asdfasdfsd</a>====alert (1111) echo strip_tags ($STR); Results: Sdfasdfsd====alert (1111)
If you want to filter all the labels strip_tags is enough.