Thread: Wildcard Finder
View Single Post
  #1  
Old 03-10-2009, 11:53 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Wildcard Finder

I got bored, so I made this simple wildcard finder.

PHP Code:
// Made by Chompy
/* 
   Only supports one wildcard!

   find(str, arr[], bool)

   Syntax defines what to search for.

   Array is a member of all strings to compare with.

   Strict determines if it will use stricter filter rules on output.
   ("foo*" would not return "foo" in the output array for example)

*/
function find(syntax, array, strict) {
  
temp.pos = syntax.pos("*");
  if (
pos == -1) {
    return {array[array.
index(syntax)]};
  }
  else {
    
temp.a = 0; temp.b = ""; temp.c = "";
    if (
pos < 1) { // *x
      
a = 1;
      
c = syntax.substring(1);
    }
    if (!(
pos == syntax.length()-1) && a == 0) { // x*x
      
a = 2;
      
c = syntax.substring(pos+1);
      
b = syntax.substring(0, syntax.length()-1-c.length());
    }
    if (
pos == syntax.length()-1) { // x*
      
a = 3;
      
b = syntax.substring(0, pos);
    }
    if (
a > 0) {
      
temp.out = 0;
      
temp.compare = syntax.substring(0, pos);
      
compare @= syntax.substring(pos+1);
      for(
temp.i : array) {
        if (
strict && i == compare) continue;
        if (
a == 1) {
          if (
i.ends(c)) out.add(i);
        }
        else if (
a == 2) {
          if (
i.starts(b) && i.ends(c)) out.add(i);
        }
        else if (
a == 3) {
          if (
i.starts(b)) out.add(i);
        }
      }
      return 
out;
    }
  }
  return 
0;
} 
Example:

PHP Code:
function onCreated() { 
  
temp.a = {"foo", "foo1", "bar", "barfoo", "baroo"};
  
  
temp.b = find("foo*", a); // foo,foo1
  
temp.c = find("foo*", a, true); // foo1,
  
  
temp.d = find("bar*oo", a); // barfoo,baroo
  
temp.e = find("bar*oo", a, true); // barfoo
  
  
temp.f = find("*o", a); // foo,barfoo,baroo
  
  
temp.g = find("*", a); // foo,foo1,bar,barfoo,baroo
} 
Please post any bugs and criticism below I guess.
__________________
Reply With Quote