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.
I tend to use strpos
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
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 givenor 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
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.
Or, use preg_match_all to see how many matches there are, total.
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.