SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    Non-Member
    Join Date
    Oct 2005
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Wildcard array search

    Hi, I have been trying to get this code to work.

    I have 2 arrays:

    $search = array('contact', 'feedback', 'help', 'support');

    The other array is generated dynamically, named $all, is much larger seen below...

    Array ( [0] => http://www.providesupport.com?messen...omleadssupport [1] => http://www.customleads.net/phone-verified.htm [2] => http://www.customleads.net/female-verified.htm [3] => http://www.customleads.net/surveyed.htm) truncated for display....

    I need to be able to return all occurences of $all that match all of the values in $search. The value of search can appear anywhere in the array, so I need to actually query for *contact*, *feedback*, etc....

    I've located this code below which may work? If someone could help me apply it to my situation, it would be a big help.

    function in_wildarray($needle, $haystack, $case_sensitive=false) {
    $is_wild = (!strpos($needle,"*")) ? false : true;
    $needles = ($is_wild) ? explode("*", $needle) : array();
    $needle = ($case_sensitive) ? $needle : strtolower($needle);
    for($i=0, $req = count($haystack);$i < $req; ++$i) {
    $haystack_str = ($case_sensitive) ? $haystack[$i] : strtolower($haystack[$i]);
    if ($is_wild) {
    $found = false;
    for($x=0, $req = count($needles); $x < $req;++$x) {
    $needle_index = strpos($haystack_str, trim($needles['x']));
    $found = (!$needle_index) ? false : ($haystack_str = substr($haystack_str, 0, $needle_index ? true : false));
    }
    if ($found) return true;
    } elseif (!$is_wild && $haystack_str == $needle) {
    return true;
    }
    }
    return false;
    }

  2. #2
    SitePοint Troll disgracian's Avatar
    Join Date
    Aug 2006
    Location
    Samsara
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function in_wildarray($search$all) {
        
    $reg_str join("|"$search);
        
    $matches = array();
        
        foreach(
    $all as $element) {
            if(
    eregi($reg_str$element) {
                
    array_push($matches$element);
            }
        }
        
        return 
    $matches;

    The parameters passed are $search and $all, the two arrays you mentioned in your OP. $matches is the subset of $all that contains the values in $search.

    DISCLAIMER: I'm not a PHP programmer, so take this with a grain of salt. But for what you're asking this shouldn't be far from the mark.

    Cheers,
    D.

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function in_array_wildcard $needle$arr )
    {
        return 
    array_valuespreg_grep'/' str_replace'*''.*'$needle ) . '/'$arr ) );

    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    SitePοint Troll disgracian's Avatar
    Join Date
    Aug 2006
    Location
    Samsara
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Shouldn't the str_replace function include the $needle variable?

    Cheers,
    D.

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    That it should...
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  6. #6
    Non-Member
    Join Date
    Oct 2005
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    PHP Code:
    function in_array_wildcard $needle$arr )
    {
        return 
    array_valuespreg_grep'/' str_replace'*''.*'$needle ) . '/'$arr ) );

    I've tried this with strange results. Here is my code below:

    PHP Code:
        print_r($all);
        echo 
    "<br><br>";
        
        
    #################################

    /*** an array of values to search for ***/
    $search = array('contact''feedback''help''support''about');

     function 
    in_array_wildcard $needle$arr ){    
     return 
    array_valuespreg_grep'/' str_replace'*''.*'$needle ) . '/'$arr ) );
     } 


    $test in_array_wildcard($search,$all);



    print_r($test); 
    Below is my output, notice no matching is taking place.

    PHP Code:
    Array ( [0] => http://www.corenutri.com [1] => http://www.corenutri.com/index.php?option=com_frontpage&Itemid=1 [2] => http://www.corenutri.com/index.php?option=com_content&task=view&id=5&Itemid=6 [3] => http://www.corenutri.com/index.php?option=com_content&task=view&id=12&Itemid=27 [4] => http://www.corenutri.com/index.php?option=com_content&task=view&id=14&Itemid=29 [5] => http://www.corenutri.com/index.php?option=com_content&task=view&id=15&Itemid=30 [6] => http://www.corenutri.com/index.php?option=com_content&task=view&id=16&Itemid=31 [7] => http://www.corenutri.com/index.php?option=com_content&task=view&id=17&Itemid=32 [8] => http://www.corenutri.com/index.php?option=com_content&task=view&id=18&Itemid=33 [9] => http:/javascript:document.loginForm.submit(); [10] => http://www.corenutri.com/index.php?option=com_registration&task=lostPassword [11] => http://www.3den.org/ [12] => http://www.herbalife.com/ecm/targeted-nutrition/targeted_nutrition.jsp [13] => http://www.herbalife.com/ecm/energy-fitness/energy_fitness.jsp [14] => http://www.herbalife.com/ecm/targeted-nutrition/targeted_nutrition.jsp [15] => http://www.herbalife.com/ecm/personal-care/personal_care.jsp [16] => http://www.herbalife.com/global/science/science_landing.jsp [17] => http://www.herbalife.com/dsassign/business_opportunity_landing.jsp [18] => http://www.herbalife.com/global/about-herbalife/about_landing.jsp [19] => http://www.herbalife.com/ecm/success/success.jsp?category=weight_mgmt&pageId=1146678444950:en_US [20] => http://www.herbalife.com/catalog/catalog.jsp?_requestid=510645 [21] => http://www.herbalife.com/dsassign/business_opportunity_landing.jsp?_requestid=61440 [22] => http://www.herbalife.com/global/about-herbalife/our_history.jsp [23] => http://www.herbalifefamily.org/ [24] => http://www.herbalife.com/global/about-herbalife/hl_worldwide.jsp?_requestid=209152 [25] => http://hbnengam.herbalife.com/ [26] => http:/videos/Corporate_Fact_Sheet061507.pdf [27] => http://www.linkreferral.com/adwel.pl?oldrefid=181703 [28] => http://cc.msnscache.com/ [29] => http://cc.msnscache.com/ [30] => http://cc.msnscache.com/ [31] => http://cc.msnscache.com/ [32] => http://cc.msnscache.com/ [33] => http://cc.msnscache.com/ [34] => http://cc.msnscache.com/ [35] => http://cc.msnscache.com/ [36] => http://cc.msnscache.com/ [37] => http://cc.msnscache.com/ [38] => http://cc.msnscache.com/ [39] => http://cc.msnscache.com/ [40] => http://cc.msnscache.com/ [41] => http://cc.msnscache.com/ [42] => http://cc.msnscache.com/ [43] => http://cc.msnscache.com/ [44] => http://cc.msnscache.com/ [45] => http://cc.msnscache.com/ [46] => http://cc.msnscache.com/ [47] => http://cc.msnscache.com/ [48] => http://cc.msnscache.com/ [49] => http://cc.msnscache.com/ [50] => http://cc.msnscache.com/ [51] => http://cc.msnscache.com/ [52] => http://cc.msnscache.com/ [53] => http://cc.msnscache.com/ [54] => http://cc.msnscache.com/ [55] => http://cc.msnscache.com/ [56] => http://cc.msnscache.com/ [57] => http://www.youtube.com/watch/ [58] => http://www.youtube.com/watch/ [59] => http://www.youtube.com/watch/ [60] => http://www.youtube.com/watch/ [61] => http://www.youtube.com/watch/ [62] => http://www.youtube.com/watch/ [63] => http://www.youtube.com/watch/ [64] => http://www.youtube.com/watch/ [65] => http://www.youtube.com/watch/ [66] => http://www.youtube.com/watch/ [67] => http://www.youtube.com/watch/ [68] => http://www.youtube.com/watch/ [69] => http://www.youtube.com/watch/ [70] => http://www.youtube.com/watch/ [71] => http://www.youtube.com/watch/ [72] => http://www.youtube.com/watch/ [73] => http://www.youtube.com/watch/ [74] => http://www.youtube.com/watch/ [75] => http://www.youtube.com/watch/ [76] => http://www.youtube.com/watch/ [77] => http://www.youtube.com/watch/ [78] => http://www.youtube.com/watch/ [79] => http://www.youtube.com/watch/ [80] => http://www.youtube.com/watch/ [81] => http://www.youtube.com/watch/ [82] => http://www.youtube.com/watch/ [83] => http://www.youtube.com/watch/ [84] => http://www.youtube.com/watch/ [85] => http://www.youtube.com/watch/ [86] => http://www.youtube.com/watch/Array [87] => http://finance.google.com/finance/ [88] => http://finance.google.com/finance/ [89] => http://finance.google.com/finance/ [90] => http://finance.google.com/finance/ [91] => http://finance.google.com/finance/ [92] => http://finance.google.com/finance/ [93] => http://finance.google.com/finance/ [94] => http://finance.google.com/finance/ [95] => http://finance.google.com/finance/ [96] => http://finance.google.com/finance/ [97] => http://finance.google.com/finance/ [98] => http://finance.google.com/finance/ [99] => http://finance.google.com/finance/ [100] => http://finance.google.com/finance/ [101] => http://finance.google.com/finance/ [102] => http://finance.google.com/finance/ [103] => http://finance.google.com/finance/ [104] => http://finance.google.com/finance/ [105] => http://finance.google.com/finance/ [106] => http://finance.google.com/finance/ [107] => http://finance.google.com/finance/ [108] => http://finance.google.com/finance/ [109] => http://finance.google.com/finance/ [110] => http://finance.google.com/finance/ [111] => http://finance.google.com/finance/ [112] => http://finance.google.com/finance/ [113] => http://finance.google.com/finance/ [114] => http://finance.google.com/finance/ [115] => http://finance.google.com/finance/ [116] => http://finance.google.com/finance/ [117] => http://finance.google.com/finance/Array [118] => http://cc.msnscache.com/ [119] => http://cc.msnscache.com/ [120] => http://cc.msnscache.com/ [121] => http://cc.msnscache.com/ [122] => http://cc.msnscache.com/ [123] => http://cc.msnscache.com/ [124] => http://cc.msnscache.com/ [125] => http://cc.msnscache.com/ [126] => http://cc.msnscache.com/ [127] => http://cc.msnscache.com/ [128] => http://cc.msnscache.com/ [129] => http://cc.msnscache.com/ [130] => http://cc.msnscache.com/ [131] => http://cc.msnscache.com/ [132] => http://cc.msnscache.com/ [133] => http://cc.msnscache.com/ [134] => http://cc.msnscache.com/ [135] => http://cc.msnscache.com/ [136] => http://cc.msnscache.com/ [137] => http://cc.msnscache.com/ [138] => http://cc.msnscache.com/ [139] => http://cc.msnscache.com/ [140] => http://cc.msnscache.com/ [141] => http://cc.msnscache.com/ [142] => http://cc.msnscache.com/ [143] => http://cc.msnscache.com/ [144] => http://cc.msnscache.com/ [145] => http://cc.msnscache.com/ [146] => http://cc.msnscache.com/ [147] => http://cc.msnscache.com/ [148] => http://cc.msnscache.com/ [149] => http://cc.msnscache.com/ ) 

    Array ( [0] => http://www.youtube.com/watch/Array [1] => http://finance.google.com/finance/Array ) 
    There are only 2 values in the $test array, neither of which match anything. It should be finding the "about" links in this example.

  7. #7
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    It was merly an example for you to adjust to your needs...
    PHP Code:
    $b = array( 'contact''feedback''help''support''about' );
    $a = array( ...long... );

    function 
    some_func_name $arr$needles )
    {
        
    $reg '/.*(?:' join$needles'|' ) . ').*/i';
        return 
    preg_grep$reg$arr );
    }

    var_dumpsome_func_name$a$b ) ); 
    Returns:
    Code:
    array
      18 => string 'http://www.herbalife.com/global/about-herbalife/about_landing.jsp' (length=65)
      22 => string 'http://www.herbalife.com/global/about-herbalife/our_history.jsp' (length=63)
      24 => string 'http://www.herbalife.com/global/about-herbalife/hl_worldwide.jsp?_requestid=209152' (length=82)
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  8. #8
    Non-Member
    Join Date
    Oct 2005
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much. That's perfect!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •