PHP string matching

Discuss Programming
Post Reply
agent007
administrator
administrator
Posts: 254
Joined: Wed Feb 12, 2003 11:26 pm

PHP string matching

Post by agent007 » Mon May 03, 2004 12:54 pm

hi guys,

Was wondering if someone could pls help me out here...I need to compare a string of characters like this 'digital principles and applications' to a word like 'digital'

Now, if the word 'digital' exists in the string, which it does, then the condition should be true, otherwise it should be false. The string will be stored in a variable like this; [the string could be anything]

$text= digital principles and applications;
$word= digital;

I tried using the "substr_count" function, but that seems to work only with arrays and there are no arrays here..

TIA..

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Mon May 03, 2004 2:03 pm

http://us4.php.net/manual/en/function.preg-match.php

Code: Select all

<?
  $word = "this";
  $sentence = "This is a test!";
  if (preg_match("/$word/i",$sentence)) { print "Found\n"; } else { print "Not Found\n"; }
?>

agent007
administrator
administrator
Posts: 254
Joined: Wed Feb 12, 2003 11:26 pm

Post by agent007 » Tue May 04, 2004 12:50 pm

Super! Thanks for that Void....I find it confusing to figure exactly which function to use...

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Tue May 04, 2004 1:24 pm

I find it that way with anything I am unfamiliar with. Once you get familiar with it, it becomes less confusing. :)

User avatar
xyle_one
programmer
programmer
Posts: 129
Joined: Mon Jan 13, 2003 1:02 pm
Contact:

Post by xyle_one » Tue May 04, 2004 9:16 pm

I might recommed the "O'Reilly - PHP Cookbook". It has been a life saver in my quest to learn PHP. get me on AIM (eskador1), or PM me if you don't mind sifting through a PDF of it. Unless this is not allowed, in which case I would appreciate it if soemone edited this last part out ;)

Post Reply