home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 1701
Discussions: 188,401, Posts: 2,243,608, Members: 232,631
Old September 9th, 2005, 08:17 PM   Digg it!   #1 (permalink)
Ultimate Member
 
golfcart's Avatar
 
Join Date: Oct 2001
Location: Michigan
Posts: 1,767
Need easy PHP help

lets say I have a variable...


$sentance = "the computer is on";


...and I want to know if it contains the word "computer". How would I do that?

I want to do an if then statement based on wether or not the variable contains a certain word. Like this:

$sentance = "the computer is on";
$word = "computer";
if ($sentence contains $word) { //This is the line I need help with
header( "location: bad.php" );
} else {
header( "location: good.php" );
}


I know it's simple but I'm stumped


Last edited by golfcart : September 9th, 2005 at 08:20 PM.
golfcart is offline   Reply With Quote
Old September 11th, 2005, 05:37 AM     #2 (permalink)
Member
 
Join Date: Jul 2005
Location: Malaysia
Posts: 132
// maybe you're looking for these...
// both are using the regular expression

/***
this one will load either bad.php or good.php when the $word is found within $sentance
***/
/********** somename.php **********/
<?php

$sentance = "the computer is on";
$word = "computer";

if( ereg( $word, $sentance ) && ereg( "computer", $sentance ) ){
print header( "location: bad.php" );
}
else{
print header( "location: good.php" );
}

?>
/********** end somename.php **********/

/***
this one will print either
1) $sentance contains $word.
2) $sentance does not contain $word!
***/
/********** some-other-name.php **********/
<html>
<head>
<title>Does $sentance contain $word/"computer"?</title>
</head>

<body>

<?php

$sentance = "the computer is on";
$word = "computer";

if( ereg( $word, $sentance ) && ereg( "computer", $sentance ) ){
print( "<p>'$sentance' contains <strong>$word</strong>.</p>\n" );
}
else{
print( "<p>'$sentance' does not contain <strong>$word</strong>!</p>\n" );
}

?>

</body>
</html>
/********** end some-other-name.php **********/

/*****
i hope the $sentence you typed in your post was a typo
that means $sentence was actually $sentance
if it wasn't, then you have to give it in a little bit details
*****/
__________________
I learnt from the best. YOU are amongst them!

LetMeKnow is offline   Reply With Quote
Old September 11th, 2005, 07:01 AM     #3 (permalink)
dword to your moms
 
krohnjw's Avatar
 
Join Date: Oct 2001
Location: ~/
Posts: 3,195
Send a message via AIM to krohnjw
IMO strstr is a much easier function than reinventing the wheel.

if (strstr("Does computer exist", "computer")) -> strstr will return true and you will enter the conditional.

So, a much simpler way of writing that is:

Code:
if (strstr($sentance, $word) {
     header("Location: good.php");
     exit;
}
else {
     header("Location: bad.php");
     exit;
}


Last edited by krohnjw : September 11th, 2005 at 07:03 AM.
krohnjw is offline   Reply With Quote
Old September 11th, 2005, 12:28 PM     #4 (permalink)
Member
 
Join Date: Jul 2005
Location: Malaysia
Posts: 132
Talking

well, yes i know that.

i wrote this -->>if( ereg( $word, $sentance ) && ereg( "computer", $sentance ) ){

just to show golfcart that he/she got two options...
either use the $word, or type in directly that "computer" - the word which is to be searched/identified from the $sentance... or $sentence ?...

but, here is the simple one from the above...

if( ereg( $word, $sentance ) {
header( "location: bad.php" );
}
else{
header( "location: good.php" );
}

and there it goes!

Last edited by LetMeKnow : September 11th, 2005 at 10:08 PM. Reason: typo... again...
LetMeKnow is offline   Reply With Quote
Old September 21st, 2005, 06:51 PM     #5 (permalink)
Ultimate Member
 
golfcart's Avatar
 
Join Date: Oct 2001
Location: Michigan
Posts: 1,767
Sorry for the delay guys. I was able to do what I wanted with your examples. I really appreciate it
golfcart is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
index.php?mode=something.php RyanD Webmastering and Programming 16 October 20th, 2004 11:20 PM
Easy Come, Easy Go: $50 Million Jackpot Gone no1_vern IMO Community 1 September 12th, 2003 04:47 PM
Really Easy One.... tenor_david Storage Related 5 May 29th, 2002 04:16 AM
yo what program can make PHP websites easy? Chooco Webmastering and Programming 3 March 29th, 2002 06:18 AM

Most Active Discussions
Is It Just Me? (2906)
3-days in and no threads about Gaza (161)
Unarmed man on his stomach shot by .. (6)
New Build ( Finally ) (7)
CPU wont boot (7)
Building a gaming computer advice (5)
I think I just killed my computer w.. (24)
RCA 52Inch HDTV wont turn on (5)
Folderchat Weekday thread (444)
Recent Discussions
Laptop proccesor to desktop mob.. (2)
Please help! multiple problems! (4)
RCA 52Inch HDTV wont turn on (5)
New Build ( Finally ) (7)
Common Spyware Solutions (97)
How do you move a hard-drive to.. (4)
What is the best external enclo.. (0)
Partition Magic 7.0 (Unallocate.. (17)
For cheap price and good qualit.. (1)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 04:09 AM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28