]> git.nbdom.net Git - nb.git/commitdiff
str_match
authorNicolas Boisselier <nicolas.boisselier@semantico.com>
Wed, 29 Jun 2016 14:31:30 +0000 (15:31 +0100)
committerNicolas Boisselier <nicolas.boisselier@semantico.com>
Wed, 29 Jun 2016 14:31:30 +0000 (15:31 +0100)
lib/php/nb.php

index 8e9d0cd40598ee5d82471d04a4f8045f83ae072c..c5470a1cf7ab3a010b621959cfdb1877898e65b7 100644 (file)
@@ -1,11 +1,19 @@
 <?php
 if (!defined('NB_ROOT')) define('NB_ROOT',realpath(dirname(__FILE__).'/../..'));
-#die($_REQUEST['prod']);
 if (!defined('NB_EOL')) define('NB_EOL',defined('NB_PROD') ? '' : "\n");
-#if (!defined('NB_EOL')) define('NB_EOL',"\n");
 require_once(realpath(dirname(__FILE__).'/functions.php'));
-#print_r(self::p());
 #$nb = new NB(); debug($nb->test());
+/*
+if (empty($_SERVER['DOCUMENT_ROOT'])) var_export([
+  '~0    : 0' => nb::str_match('~0','0'),
+  'null  : 0' => nb::str_match('null','0'),
+  'null  : ""' => nb::str_match('null',''),
+  '!null : ""' => nb::str_match('!null',''),
+  '!~zaz : "zaza"' => nb::str_match('!~zaz','zaza'),
+  '~zaz  : "zaza"' => nb::str_match('~zaz','zaza'),
+  'za*   : "zaza"' => nb::str_match('za*','zaza'),
+]);
+*/
 if (class_exists('NB')) return;
 class NB {
 
@@ -28,7 +36,7 @@ class NB {
   );
 
   #public static function zaza() { return (is_object($this) ? 'TRUE' : 'FALSE'); }
-  public static function test() { return 'TEST'; }
+  #public static function test() { return 'TEST'; }
   public function __construct($opt = array()) {
     foreach ($opt as $k => $v) {
       if ( ! array_key_exists($k,$this) ) self::bye("Unknow param $k = `$v`");
@@ -529,6 +537,42 @@ class NB {
     }
   }
 
+  public static function str_match($pattern,$string) {
+      $match = false;
+
+      // No empty values
+      if (strcmp($pattern,'')==0 or $pattern=='!' or $pattern=='~') return false;
+
+      // Equal / Not Equal
+      $equal = '=';
+      $not = strpos($pattern,'!')===0 ? true : false;
+      if ($not) $pattern = substr($pattern,1);
+
+      // Regex
+      if (strpos($pattern,'~')===0) {
+        $pattern = substr($pattern,1);
+        $match = preg_match('@'.str_replace('@','\@',$pattern).'@',$string);
+
+      // Match
+      } elseif(preg_match('/['.preg_quote('*?[]!').']/',$pattern)) {
+
+        #if (strpos($pattern,'*') !== false or strpos($pattern,'') !== false
+        $match = fnmatch($pattern,$string);
+      #var_dump($not);
+
+      // Default
+      } else {
+        if (strtolower($pattern)=='null') {
+          $pattern = '';
+          if (!$string) $string = 0;
+        }
+        #debug("$pattern = $string");
+        $match = ($pattern == $string);
+      }
+
+      return (bool)($not ? !$match : $match);
+
+  }
+
 } # < Class
-#die(self::ext2mime('csv'));
 ?>