Original Title[solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sumanbangladesh
Forum Newbie
Posts: 5
Joined: Tue Sep 18, 2007 2:41 am

Original Title[solved]

Post 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
Last edited by sumanbangladesh on Tue Sep 18, 2007 9:55 pm, edited 1 time in total.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

strpos() is faster than strstr(), so I'd imagine stripos() is faster than stristr() as well.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
sumanbangladesh
Forum Newbie
Posts: 5
Joined: Tue Sep 18, 2007 2:41 am

Search Keyword is solved

Post by sumanbangladesh »

Thank you.
Post Reply