Consider:
$a = 'How are you?';
if ($a contains 'are')
echo 'true';
Suppose I have the code above, what is the correct way to write the statement if ($a contains 'are')?
|
|
You can use the strpos function which is used to find the occurrence of one string inside other:
Note that the use of |
|||||||||||||||||||||
|
|
You could use regular expressions. It would look something like this:
Don't tell me it's bad just because you've heard it's bad before. You might if you have any facts to back it up though ;) On the performance side, strpos is about three times faster and have in mind, when I did one million compares at once, it took preg match 1.5 seconds to finish and for strpos it took 0.5 seconds. What I'm trying to say is that it runs really fast either way. If you don't have 100,000 visitors every second, you shouldn't concern yourself with this kind of stuff and take what's most comfortable, IMO. |
|||||||||||||||||||||
|
|
Use strpos function:
|
|||
|
|
|
Here is a little utility function that is useful in situations like this
|
|||||||||||||||||||||
|
|
To determine whether a string contains another string you can use the PHP function strpos().
CAUTION: If the needle you are searching for is at the beginning of the haystack it will return position 0, if you do a A A |
|||||||||
|
|
While most of these answers will tell you if a substring appears in your string, that's usually not what you want if you're looking for a particular word, and not a substring. What's the difference? Substrings can appear within other words:
One way to mitigate this would be to use a regular expression coupled with word boundaries (
This method doesn't have the same false positives noted above, but it does have some edge cases of its own. Word boundaries match on non-word characters (
If you want anything more accurate than this, you'll have to start doing English language syntax parsing, and that's a pretty big can of worms (and assumes proper use of syntax, anyway, which isn't always a given). |
|||||||||||||||||||||
|
|
|
|||||||||
|
|
||||
|
|
|
Make use of case-insensitve matching using
|
||||
|
|
|
If you want to avoid the "falsey" and "truthy" problem, you can use substr_count:
It's a bit slower than strpos but it avoids the comparison problems. |
|||
|
|
|
Another option is to use the strstr() function. Something like:
Point to note: The strstr() function is case-sensitive. For a case-insensitive search, use the stristr() function. |
|||||||||
|
|
Peer to SamGoody and Lego Stormtroopr comments. If you are looking for a php algorithm to rank search results based on proximity/relevance of multiple words here comes a quick and easy way of generating search results with PHP only: Issues with the other boolean search methods sush as
PHP method based on Vector Space Model and tf-idf (term frequency–inverse document frequency): It sounds difficult but is surprisingly easy. If we want to search for multiple words in a string the core problem is how we assign a weight to each one of them? If we could weight the terms in a string based on how representative they are of the string as a whole, we could order our results by the ones that best match the query. This is the idea of the vector space model, not far from how SQL fulltext search works:
CASE 1
RESULT
CASE 2
RESULTS
CASE 3
RESULTS
There are plenty of improvements to be made
but the model provides a way of getting good results from natural queries,
which don't have boolean operators sush as NOTA BENE Optionally eliminating redundancy prior to search the words
1. Normalisation
2. Stop word elimination
3. Dictionary substitution
RESOURCES
|
||||
|
|
|
The function below also works and does not depend on any other function; it uses only native PHP string manipulation. Personally, I do not recommend this, but you can see how it works:
Test:
|
|||||||||||||||||
|
|
I'm a bit impressed that none of the answers here that used Basically, if you're having trouble finding words with characters specific to some languages, such as German, French, Portuguese, Spanish, etc. (e.g.: ä, é, ô, ç, º, ñ), you may want to precede the functions with
If you cannot guarantee that all your data is 100% in UTF-8, you may want to use the A good article to understand why is The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) by Joel Spolsky. |
||||
|
|
|
|||||||||
|
|
I had some trouble with this, and finally I chose to create my own solution. Without using regular expression engine:
You may notice that the previous solutions are not an answer for the word being used as a prefix for another. In order to use your example:
With the samples above, both |
|||||||||||||
|
|
|||||||||||||
|
|
Another option to finding the occurrence of a word from a string using strstr() and stristr() is like the following:
|
|||||
|
|
You should use case Insensitive format,so if the entered value is in
Here stripos finds needle in heystack without considering case (small/caps). |
|||||
|
|
The short-hand version
|
|||||
|
|
In order to find a 'word', rather than the occurrence of a series of letters that could in fact be a part of another word, the following would be a good solution.
|
|||||
|
|
Maybe you could use something like this:
|
||||
|
|
|
You can use the
Without using inbuilt function:-
|
||||
|
Do not use
|
||||
|
|
|
It can be done in three different ways:
1- stristr()
2- strpos()
3- preg_match()
|
|||||
|
|
You need to use identical/not identical operators because strpos can return 0 as it's index value. If you like ternary operators, consider using the following (seems a little backwards I'll admit):
|
||||
|
|
|
If you want to check if the string contains several specifics words, you can do:
This is useful to avoid spam when sending emails for example. |
||||
|
|
This means the string has to be resolved into words (see note below). One way to do this and to specify the separators is using
A run gives
Note: Here we do not mean word for every sequence of symbols. A practical definition of word is in the sense the PCRE regular expression engine, where words are substrings consisting of word characters only, being separated by non-word characters.
|
||||
|
|
|
In PHP, the best way to verify if a string contains a certain substring, is to use a simple helper function like this :
Explanation :
Output :
|
||||
|
|
|
A string can be checked with the below function:
|
|||||||||
|
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?