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 949127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:21:22+00:00 2026-05-15T23:21:22+00:00

How can you search a partial string when typing (not to use MySQL) like

  • 0

How can you search a partial string when typing (not to use MySQL) like the LIKE function in MySQL but using PHP when searching a string, e.g.

<?php    

$string = "Stackoverflow";

$find = "overfl";

if($find == $string)
{
    return true;
}
else
{
    return false
}

?>

But that will obviously work won’t, but is there a function where you can search partially of a string? That would be great 🙂

EDIT:

What if it was in an array?

if i use the strpos, it does the echo, If I use it, it goes like truetruetruetruetrue.

  • 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-15T23:21:23+00:00Added an answer on May 15, 2026 at 11:21 pm

    I tend to use strpos

    $needle='appy';
    $haystack='I\'m feeling flappy, and you?';
    
    if(strpos($haystack,$needle)!==false){
       //then it was found
       }
    

    If you want it to ignore case, use stripos.

    Note that a subtlety about this is that if the needle is at the very start of the haystack, in position 0, integer 0 is returned. This means you must compare to false, using strict comparison, or it can produce a false negative.

    As noted in the manual, linked above

    Warning

    This function may return Boolean
    FALSE, but may also return a
    non-Boolean value which evaluates to
    FALSE, such as 0 or “”. Please read
    the section on Booleans for more
    information. Use the === operator for
    testing the return value of this
    function.

    As far as using arrays, strpos is meant to take two strings. Using an array will produce Warning: strpos() expects parameter 1 to be string, array given or 1Warning: strpos(): needle is not a string or an integer`.

    Okay, let’s say you have an array of strings for which to search.

    You can

    $needles=array('hose','fribb','pancake');
    $haystack='Where are those pancakes??';
    
    foreach($needles as $ndl){
       if(strpos($haystack,$ndl)!==false){ echo "'$ndl': found<br>\n"; }
       else{ echo "'$ndl' : not found<br>\n"; }
    }
    

    Another way of searching for multiple strings in one string, without using an array… This only tells you whether at least one match was found.

    $haystack='Where are those pancakes??';
    $match=preg_match('#(hose|fribb|pancake)#',$haystack);
    //$match is now int(1)
    

    Or, use preg_match_all to see how many matches there are, total.

    $all_matches=preg_match_all('#(hose|fribb|pancake)#',$haystack,$results);
    //all_matches is int(2). Note you also have $results, which stores which needles matched.
    

    In that, the search term is a regular expression. () groups the terms together, and | means ‘or’. # denotes the beginning and end of the pattern. Regexes can get pretty complicated quickly, but of course, they work! They are often avoided for performance reasons, but if you’re testing multiple strings, this might be more efficient than they array looping method described above. I’m sure there are also other ways to do this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to search for a partial word using Solr, but I can't get
This is quite weird but my search function can search for any word except
After my previous question (http://stackoverflow.com/questions/8217522/best-way-to-search-for-partial-words-in-large-mysql-dataset), I've chosen Sphinx as the search engine above my
So I can search the whole database but i need a way to change
I want to write a php code that can search for multiple keywords separated
I'm trying to use an NSPredicate to search for string matches against an array
I can't figure out why my OnPreLoad function doesn't get fired for search.aspx.cs. *
Searching for a string within a string is extremely well supported in .NET but
I can get this working with an XmlDataSource but not with my own classes.
I would like to be able to use Full text search in Code First

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.