Php search string (with wildcards)

后端 未结 5 799
自闭症患者
自闭症患者 2021-01-05 03:27

Is there a way to put a wildcard in a string? The reason why I am asking is because currently I have a function to search for a substring between two substrings (i.e grab th

相关标签:
5条回答
  • 2021-01-05 03:56

    I agree that regex are much more flexible than wildcards, but sometimes all you want is a simple way to define patterns. For people looking for a portable solution (not *NIX only) here is my implementation of the function:

    function wild_compare($wild, $string) {
        $wild_i = 0;
        $string_i = 0;
    
        $wild_len = strlen($wild);
        $string_len = strlen($string);
    
        while ($string_i < $string_len && $wild[$wild_i] != '*') {
            if (($wild[$wild_i] != $string[$string_i]) && ($wild[$wild_i] != '?')) {
                return 0;
            }
            $wild_i++;
            $string_i++;
        }
    
        $mp = 0;
        $cp = 0;
    
        while ($string_i < $string_len) {
            if ($wild[$wild_i] == '*') {
                if (++$wild_i == $wild_len) {
                    return 1;
                }
                $mp = $wild_i;
                $cp = $string_i + 1;
            }
            else
            if (($wild[$wild_i] == $string[$string_i]) || ($wild[$wild_i] == '?')) {
                $wild_i++;
                $string_i++;
            }
            else {
                $wild_i = $mp;
                $string_i = $cp++;
            }
        }
    
        while ($wild[$wild_i] == '*') {
            $wild_i++;
        }
    
        return $wild_i == $wild_len ? 1 : 0;
    }
    

    Naturally the PHP implementation is slower than fnmatch(), but it would work on any platform.

    It can be used like this:

    if (wild_compare('regex are * useful', 'regex are always useful') == 1) {
        echo "I'm glad we agree on this";
    }
    
    0 讨论(0)
  • If you insist to use wildcard (and yes, PREG is much better) you can use function fnmatch which works only on *NIX.

    Cheers

    0 讨论(0)
  • 2021-01-05 03:58

    Use a regex.

    $string = "My dog has fleas";
    if (preg_match("/\S+ (\S+) has fleas/", $string, $matches))
      echo ($matches[1]);
    else
      echo ("Not found");
    

    \S means any non-space character, + means one or more of the previous thing, so \S+ means match one or more non-space characters. (…) means capture the content of the submatch and put into the $matches array.

    0 讨论(0)
  • 2021-01-05 04:00

    wildcard pattern could be converted to regex pattern like this

    function wildcard_match($pattern, $subject) {
      $pattern = strtr($pattern, array(
        '*' => '.*?', // 0 or more (lazy) - asterisk (*)
        '?' => '.', // 1 character - question mark (?)
      ));
      return preg_match("/$pattern/", $subject);
    }
    

    if string contents special characters, e.g. \.+*?^$|{}/'#, they should be \-escaped

    don't tested:

    function wildcard_match($pattern, $subject) {
      // quotemeta function has most similar behavior,
      // it escapes \.+*?^$[](), but doesn't escape |{}/'#
      // we don't include * and ?
      $special_chars = "\.+^$[]()|{}/'#";
      $special_chars = str_split($special_chars);
      $escape = array();
      foreach ($special_chars as $char) $escape[$char] = "\\$char";
      $pattern = strtr($pattern, $escape);
      $pattern = strtr($pattern, array(
        '*' => '.*?', // 0 or more (lazy) - asterisk (*)
        '?' => '.', // 1 character - question mark (?)
      ));
      return preg_match("/$pattern/", $subject);
    }
    
    0 讨论(0)
  • 2021-01-05 04:16

    This is one of the few cases where regular expressions are actually helpful. :)

    if (preg_match('/my (\w+) has/', $str, $matches)) {
        echo $matches[1];
    }
    

    See the documentation for preg_match.

    0 讨论(0)
提交回复
热议问题