I'm want to make a script that takes the referral keyword/phrase and shows a certain piece of text depending on word that is contained in keyword phrase. Basically something like this: IF $keyword contains 'basketball'; Then $text = 'do you like basketball'; I guess i just need a IF string contains function... not sure how to do that.
i looked that up but from what i understand the output is a number... display the position of the keyword within the phrase. I need it to echo text if the keyword is found within the string. Maybe I'm just not understanding correctly.
in theory, you could just do if(strpos(whatever)) now, since any number is boolean true, but remember that the word could be at the position 0. which would be interpreted as false, even though there is a match so what you do is if(strpos(whatever) !== false)
Here's a little snippet of code you might like; <?php $keyword = 'basketball practice'; preg_match('#(basketball|baseball|football)#i', $keyword, $match); $matched = $match[0]; switch ($matched) { case 'basketball': $text = 'Do you like basketball?'; break; case 'baseball': $text = 'Do you like baseball?'; break; case 'football': $text = 'Do you like football?'; break; default: $text = 'What sport do you like?'; break; } echo $text; ?> PHP: What this does is searches the $keyword variable for the patterns in the preg_match function. Just add a new pipe character along with a keyword to match more keywords. Then add another case statement to the switch according to the new keyword you added.
if he wants just one word, preg_match is inefficient. Also, that code would ignore multiple matches. strpos is the way to go (or maybe preg_match_all, but I don't think that'd be very efficient)
Wow, epic fail. You NEVER use preg_match for THIS. You use strpos. Do you have any idea how resource intensive regular expressions are? And besides, how the fuck is he supposed to learn anything when you serve it to him on a silver platter? Do you want him to come back here asking every time he needs an if clause written? Or do you actually want to TEACH him?
Give a man a fish, he knows where to get fish. Teach a man to fish, you've just lost a customer & gained a competitor.
Well, since someone wants to be a critic, I've rewritten this to be a bit more suitable: <?php #default text if no matches are phone $text = 'What sport do you like?'; $phrase = 'basketball practice'; $keywords = array( 'basketball' => 'Do you like basketball?', 'baseball' => 'Do you like baseball?', 'football' => 'Do you like football?' ); foreach ($keywords as $keyword => $value) { if (strpos($phrase, $keyword) !== false) { $text = $value; } } echo $text; ?> PHP: This will allow you to easily add keywords and text results for each of them, by changing the values in the array. It loops through the array and utilizes strpos to determine whether or not the keyword is contained in the phrase, and if it, it sets the $text variable and echoes it.
Just to clarify, I am trying to learn... I read quite a few things before just posting on here. I'm somewhat following the code you've displayed, the thing is it seems like I would have to know what the string is. Let me show you guys what I had before even coming here: $parse = parse_url($_SERVER['HTTP_REFERER']); $se = $parse["host"]; $raw_var = explode("&", $parse["query"] ); foreach ($raw_var as $one_var) { $raw = explode("=", $one_var); $var[$raw[0]] = urldecode ($raw[1]); } $se = explode (".", $se); switch ($se[1]) { case 'yahoo': $keywords = $var['p']; break; case 'aol': $keywords = $var['query']; break; default: $keywords = $var['q']; } unset($parse, $se, $raw_var, $one_var, $var); switch ($keywords) { //create message case "basketball": $message = 'Here are some quality basketball resources'; break; case "baseball": $message = 'Wow, you like baseball... that is great'; break; case "football": $message = 'Football is my favorite sport'; break; } PHP: As you can probably see, this won't work because unless case matches exactly Baseball, basketball, or football the message won't display. I need to figure out if how to display a message if (for example): $keywords = LA Lakers Basketball
You could try changing the switch statement a bit. Wrap strtolower around $keywords like so: switch (strtolower($keywords)) { PHP:
i figure out how to do it like this: if (stristr($keywords,'basketball')) { $message = 'basketball is awesome'; } PHP: But instead of using a bunch of else if commands how would I use switch instead?
I misunderstood what you were trying to accomplish with my last post, sorry about that. I got a little creative with this and believe I've produced an efficient result: <?php #the key of the array is the keyword used, and the value is the message delivered from a match $messages = array( 'basketball' => 'Here are some quality basketball resources', 'baseball' => 'Wow, you like baseball... that is great', 'football' => 'Football is my favorite sport', 'What sports do you like?' #this is the message shown if no matches are found ); #the key is the host of the referer, the value is the variable in the query params $variables = array( 'yahoo' => 'p', 'aol' => 'query', 'q' #this is the variable used if no matches are found ); $parse = parse_url($_SERVER['HTTP_REFERER']); parse_str($parse['query'], $query); #more efficient replacement of your use of explode() and foreach() - $query is brought into the current scope list($host, $blank) = explode('.', $parse['host'], 2); #more efficient solution in my opinion, $host and $blank (unused) are brought into the scope $keywords = $query[$variables[$host]]; #keywords from the referer unset($variables, $parse, $host, $blank, $query); #clean it up foreach ($messages as $keyword => $message) { #$keyword is the key from each item in $messages, $message is the value if (stripos($keywords, $keyword) !== false && is_string($keyword)) { #check for a match and make sure that $keyword is a string break; #kill it when a match is found } } #$message stays registered as the valid message from $messages, $key also stays registered as the keyword echo $message; ?> PHP: Note: stripos is not a typo, it's the case-insensitive version of strpos. It's a little advanced, but I believe it gives you a chance to check out some new functions you might not have known about. I've notated it as best as I could to explain, I hope it helps