![]() |
How to > if values in array exist in the sentence ?
Hello,
I want to do something if my sentence include one of the words in this array, How to do that ? PHP Code:
|
Regular expressions. Or if you want to go wal-mart cheap, explode the string into an array on spaces and use in_array(), but then you can't search for fragments.
|
PHP Code:
|
Well that's probably because you're looking for an array in a string. Also, once you switch the arguments of in_array(), you're suddenly looking for each word of the original string inside of, you guessed it, the original string. At least with the example code you posted, but you may have done that from memory and mixed it up, I'm not sure.
The above method I suggested WILL work if done correctly, but it starts to get complicated once you start factoring in capture of words with punctuation attached, or if you want to find word fragments. Your best bet is to look into regular expressions - the basics are easy to pick up, and from there you can get as wild as you want. For example, a simple search could be done ala; php Code:
|
delayedinsanity, your code works nice. But how to make it no-case-sensitive ?
Thanks. |
Info link: *Click me*
Here you can find: 'The "i" after the pattern delimiter indicates a case-insensitive search' |
I tried this but it doesnt work with this script ?
PHP Code:
|
It's in the wrong spot.
Regular expressions use delimiters; characters that tell the compiler where the expression starts, and where it ends. Common delimeters are /, ~ or #, though it can technically be anything you choose. In the example I provided, the delimiter is ~, so the compiler will see the expression as; php Code:
The expression modifiers, such as i, need to be placed after the closing delimiter. php Code:
|
Thank you, this it what I need.
PHP Code:
|
| All times are GMT. The time now is 03:02 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0