PHP several useful custom Function summary, PHP Custom Function Summary _php tutorial

Source: Internet
Author: User

PHP several useful custom Function summary, PHP Custom Function Summary


This paper summarizes several useful custom functions of PHP. Share to everyone for your reference, as follows:

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 ("Submarine Eagle Blog", 7); The result is the seabed, utf8 a Chinese character corresponding 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 = "asdfasdfsd===="; Echo deltags ($STR);  Results: Asdfasdfsd====alert (1111) echo strip_tags ($STR); Results: Sdfasdfsd====alert (1111)

If you want to filter all the labels strip_tags is enough.

More interested in PHP related content readers can view the topic: "PHP Basic Grammar Introductory Tutorial", "PHP Object-oriented Programming tutorial" and "PHP common functions and Skills summary"

I hope this article is helpful to you in PHP programming.

Articles you may be interested in:

    • Thinkphp Custom Function solves the method of adding and subtraction of template label
    • Generate UUID Custom function sharing in PHP
    • PHP randomly generates a unique hash value custom function
    • PHP implementation of download remote image Custom Function sharing
    • PHP cross-platform Get server IP address Custom function sharing
    • PHP Statistical Directory size Custom Function sharing
    • PHP recursive replication, mobile directory Custom function sharing
    • Variable output, custom function and judgment statement usage of thinkphp template
    • PHP implementation of mobile phone number middle four-bit with an asterisk (*) hidden custom function sharing
    • PHP uses a custom function to implement a method of traversing all files in a directory
    • Convert URL addresses in text to clickable-linked JavaScript, PHP custom functions
    • PHP generation is shared with thumbnail classes and custom functions
    • PHP Custom Function intercepts the length of Chinese characters
    • PHP generates random password Custom function code (simple and fast)

http://www.bkjia.com/PHPjc/1095689.html www.bkjia.com true http://www.bkjia.com/PHPjc/1095689.html techarticle PHP Several practical custom Function summary, PHP Custom Function Summary In this paper, we summarize some useful custom functions of PHP. Share to everyone for your reference, as follows: Recently in the Watch generation ...

  • Related Article

    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.