Page 1 of 1
Original Title[solved]
Posted: Tue Sep 18, 2007 4:29 am
by sumanbangladesh
I have a variable $var="this is a long string..............some thing more"; I have to search if this variable contains the keyword "long" is present or not. Could u help me.
Thanks in advance
Posted: Tue Sep 18, 2007 4:34 am
by CoderGoblin
The normal method for looking for something like this (assuming you want 'long' as a word, not within another word) is to use the command
preg_match. Pattern syntaxes can get quite complicated but it worth your while to read
Preg Pattern Syntax and get to terms with how things work rather than just giving you a solution without you understanding how it works.
Another solution if you don't care about 'long' being within another word is to use
stristr. It will find it if part of another word, followed by ',', '.' or space etc.
Posted: Tue Sep 18, 2007 4:50 am
by aceconcepts
Here is quite a nifty little method:
Code: Select all
$test="Hello this is a test";
if(stristr($test, 'this') === FALSE)
{
echo 'no match';
}
else
{
echo "match found";
}
http://uk.php.net/manual/en/function.stristr.php
Posted: Tue Sep 18, 2007 10:25 am
by pickle
strpos() is faster than
strstr(), so I'd imagine
stripos() is faster than
stristr() as well.
Search Keyword is solved
Posted: Tue Sep 18, 2007 9:53 pm
by sumanbangladesh
Thank you.