顯示具有 PHP 標籤的文章。 顯示所有文章
顯示具有 PHP 標籤的文章。 顯示所有文章

2016年7月11日 星期一

[PHP] 常用的Regular Expression

$ipAddress = "/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])?$/";

$domain = "/^(?:(([a-z]{1})|([a-z]{1}[a-z]{1})|([a-z]{1}[0-9]{1})|([0-9]{1}[a-z]{1})|([a-z0-9][a-z0-9\-\_]{1,61}[a-z0-9]))\.)+([a-z]{2,6}|[a-z0-9-]{2,30}\.[a-z]{2,3})$/";

$macAddress = "/^\s*([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})\s*$/";

2016年7月5日 星期二

[PHP] 實作 Ping 功能

  function ping($host, $iface=null, $timeout=null) {
     $iface = ($iface===null)?"eth0":$iface;
     $timeout = ($timeout ===null)?1:$timeout;
    /* ICMP ping packet with a pre-calculated checksum */
    $package = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost";
    $socket = socket_create(AF_INET, SOCK_RAW, 1);
    socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout,  'usec'  =>  0));
    if ( @socket_set_option($socket, SOL_SOCKET, SO_BINDTODEVICE, $iface) === false ) return false;
    socket_connect($socket,  $host,  null);

    $ts = microtime(true);
    socket_send($socket,  $package,  strLen($package),  0);
    if  (socket_read($socket,  255))
      $result = microtime(true)  -  $ts;
    else $result  =  false;
    socket_close($socket);

      return $result;
    }

Usage:
ping("192.168.1.1"); #[T] 0.0005650520324707
ping("192.168.1.2"); #[F] false

Reference:
[1] http://stackoverflow.com/questions/20467432/php-how-to-ping-a-server-without-system-or-exec

[PHP] array常用方法


  • 比對array是否相同
  •     function array_equal_values(array $a, array $b, 
                             $strict = false, 
                             $allow_duplicate_values = true) {
            
            $add = (int)!$allow_duplicate_values;

            if ($add and count($a) !== count($b)) {
                    return false;
            }

            $table = [];
            $count = function (array $array) use (&$table, $add, $strict) {
                $exit   = (bool)$table;
                $result = [];
                foreach ($array as $value) {
                    $key = array_search($value, $table, $strict);

                    if (false !== $key) {
                        if (!isset($result[$key])) {
                            $result[$key] = 1;
                        } else {
                            $result[$key] += $add;
                        }
                        continue;
                    }

                    if ($exit) {
                        break;
                    }

                    $key          = count($table);
                    $table[$key]  = $value;
                    $result[$key] = 1;
              }
              return $result;
        };
        return $count($a) == $count($b);
    }

    Usage :
    [T] array_equal_values(['2.0', '2', 2], ['2', '2.0', 2], true);                
    [T] array_equal_values(['2.0', '2', 2, 2], ['2', '2.0', 2], true);
    [F] array_equal_values(['2.0', '2', 2, 2], ['2', '2.0', 2], true, false);
    [T] array_equal_values(['2'], ['2'], true, false);                  
    [T] array_equal_values([2], ['2', 2],false, true);               

  • array clone
  • function array_clone($arr) {
        $newArray = array();
        foreach($arr as $key => $value) {
             if(is_array($value)) $newArray[$key] = array_clone($value);
             else if(is_object($value)) $newArray[$key] = clone $value;
             else $newArray[$key] = $value;
        }
        return $newArray;
    }

    Usage :
    $new_arr = array_clone($old_arr);
Reference
[1] http://stackoverflow.com/questions/901815/php-compare-array