word in a text

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
pppswing
Forum Commoner
Posts: 33
Joined: Thu Jun 10, 2004 2:04 am
Location: Tallinn, Estonia

word in a text

Post by pppswing »

Hi,

I need to build a function that send me 'true' if a word is written in a text.

I don't know what php function is the best, I think that preg_match is a bit heavy.

Do you know better alternatives? a string function? :roll:

Thanks :)
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

preg_match is ok for the job. ;)
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

for simplicitys sake i highly recommend strstr

thought i'd also mention if you just needed a simple BOOL answer as to whether needle is in haystack: str_pos
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

neither of them is able to tell if a word is in the string
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Post by Chalks »

Code: Select all

preg_match("/start/i", $stringToCheck, $arrayToHoldMatches);
That will work just fine.

Or:

What malcolmboston said is true. strstr() is _very_ easy to use, so is strpos(). Just read the links he gave you.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

stereofrog wrote:neither of them is able to tell if a word is in the string
What? I hope you meant to say that BOTH of them are able to tell if a word is in a string, but not able to differentiate between an actual separated word with boundaries and just a portion of a word with the same characters.
Post Reply