PHP: a summary of some practical user-defined functions and php user-defined functions _ PHP Tutorial

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.