Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6694211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:04:31+00:00 2026-05-26T06:04:31+00:00

Is is possible to use wildcards with array_search? I want to search for part

  • 0

Is is possible to use wildcards with array_search? I want to search for part of a string and then

something like(with an asterisk)

print $pos = array_search('abitofastring%', $vars['myarray']);
unset($vars['myarray'][$pos]);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:04:32+00:00Added an answer on May 26, 2026 at 6:04 am

    Is is possible to use wildcards with array_search?

    No, but you can use regular expressions (which supports wildcards) and the preg_grep function.

    Example:

    $array = explode(',', "House,Car,Boat,Horse,Pool Boy");
    
    # remove all strings from array that do not contain "ho"
    
    $array = preg_grep('~ho~i', $array, PREG_GREP_INVERT);
    

    The array then is:

    Array
    (
        [1] => Car
        [2] => Boat
        [4] => Pool Boy
    )
    

    As it can be complicated to write regular expression patterns, it might be handy to have a helper function that converts your SQL LIKE pattern into a regular expression so this can be more easily used:

    $array = explode(',', "House,Car,Boat,Horse,Pool Boy");
    
    # Search for "Ho" at the beginning of each string
    
    $regex = like_to_regex('Ho%');
    
    $array = preg_grep($regex, $array, PREG_GREP_INVERT);
    
    print_r($array);
    
    /**
     * convert a MySQL LIKE pattern into a pcre pattern
     */
    function like_to_regex($like, $casesensitive = FALSE, $escapechar = '\\')
    {
        $pattern = sprintf('~(?<!%1$s)(%1$s{2}|%%|_)~', preg_quote($escapechar));
        $tokens = preg_split($pattern, $like, 0, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
        foreach($tokens as &$token)
        {
            switch($token)
            {
                case $escapechar.$escapechar:
                    $token = preg_quote($escapechar);
                    break;
                case '_':
                    $token = '.';
                    break;
                case '%':
                    $token = '.*';
                    break;
                default:
                    $token = preg_quote($token);
    
            }
        }
        return sprintf('~^%s$~%s', implode('', $tokens), $casesensitive ? '':'i');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is this possible to use Ajax.Beginform with update target inside of ajax form. like
I'm trying to use a RegularexpressionValidator to match an IP address (with possible wildcards)
Can/Should I use a LIKE criteria as part of an INNER JOIN when building
I am selecting some nodes using XPath, and would like to use wildcards. Is
If I have to search for some data I can use wildcards and use
I want to use Wildcards in my attributes. For example, this is my regular
Possible Duplicate: How can I use inverse or negative wildcards when pattern matching in
I'm trying to use wildcard in KVC like this. Is it possible? Or Is
Is it possible to use wildcards in the Erlang compiler's -I option? For example,
I need to let my users use asterisks (*) as wildcards in search. Is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.