TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   How to > if values in array exist in the sentence ? (http://www.talkphp.com/general/5210-how-if-values-array-exist-sentence.html)

CΛSTΞX 01-06-2010 02:00 AM

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:

$sentence "I dont give a shit";

$values = array("lick","shit","pussy","horny"); 

Thanks...

delayedinsanity 01-06-2010 02:25 AM

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.

CΛSTΞX 01-06-2010 02:30 AM

PHP Code:


$sentencearray 
explode(' ',$sentence);

foreach(
$sentencearray as $values)

if (
in_array($sentencearray$values)) {

// do something ?



Thanks, but it doesnt work.

delayedinsanity 01-06-2010 05:31 AM

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:
$string = "This is the test string.";
$stopwords = array( 'bum', 'test' );

if ( preg_match( '~(' . implode( '|', $stopwords ) . ')~', $string ) ) {
    echo 'Found!';
}

CΛSTΞX 01-22-2010 02:57 PM

delayedinsanity, your code works nice. But how to make it no-case-sensitive ?
Thanks.

Parvus 01-22-2010 03:47 PM

Info link: *Click me*

Here you can find: 'The "i" after the pattern delimiter indicates a case-insensitive search'

CΛSTΞX 01-25-2010 01:47 AM

I tried this but it doesnt work with this script ?

PHP Code:

$string "Don't look at my Ass";

$ass "/ass/i";

$stopwords = array($ass"shit""pussy");

if ( 
preg_match'~(' implode'|'$stopwords ) . ')~'$string ) ) {
    echo 
'Found!';



delayedinsanity 01-25-2010 02:21 AM

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:
preg_match( '~(word|word|word)~' ....

The expression modifiers, such as i, need to be placed after the closing delimiter.

php Code:
preg_match( '~(' . implode( '|', $stopwords ) . ')~i' ....

CΛSTΞX 01-25-2010 02:32 AM

Thank you, this it what I need.

PHP Code:


$string 
"I look for erotic pics";

if (
preg_match'~(shit|horny|erotic)~i'$string)) {

echo 
"found";





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