dennismonsewicz Posted May 27, 2009 Share Posted May 27, 2009 I have the following code: $url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; $arr = array("dog", "cat"); foreach($arr as $key) { $find = strstr($url, $key); } echo $find; it isn't working... any suggestions? Quote Link to comment Share on other sites More sharing options...
gevans Posted May 27, 2009 Share Posted May 27, 2009 $url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; $arr = array("dog", "cat"); foreach($arr as $key => $val) { $find = strstr($url, $key); } echo $find; Quote Link to comment Share on other sites More sharing options...
Zhadus Posted May 27, 2009 Share Posted May 27, 2009 gevans made the fix, but you do realize you're overwriting $find if it finds 'dog' anyway, right? Try this: $url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; $arr = array("dog", "cat"); $find = ''; foreach($arr as $key =>) { $find = strstr($url, $key); echo $find; } Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted May 27, 2009 Author Share Posted May 27, 2009 still nothing Quote Link to comment Share on other sites More sharing options...
Zhadus Posted May 27, 2009 Share Posted May 27, 2009 If you used mine instead of his, I'm an idiot $url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; $arr = array("dog", "cat"); $find = ''; foreach($arr as $key => $val) { $find = strstr($url, $val); echo $find; } Also, what exactly are you expecting and what are you getting? Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted May 27, 2009 Author Share Posted May 27, 2009 that still didn't work... I am trying to search the URL for a particular value and then I am going to create a switch. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 27, 2009 Share Posted May 27, 2009 You have to be more specific. Define not working and what you want to do. We are not psychic. Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted May 27, 2009 Author Share Posted May 27, 2009 sorry guys... It is not echoing out the $find. And if I echo out $key then I get the last variable in the array. so if dog and cat are in the array and then I echo out $key, I get cat. I need for this script to search through the URL I am grabbing for each of the values in the array. If it finds one then it will be set to a variable that I will then use to pass into my switch. I am just running some dummy code now to get the foreach statement working... Did any of that make sense? Sorry if it didn't... if you are still confused I will try to explain in more detail updated code <?php $url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; $arr = array("dog", "cat"); $find = ''; foreach($arr as $key => $val) { $find = strstr($url, $val); } ?> <p><?php echo $find; ?></p> Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 27, 2009 Share Posted May 27, 2009 So you're trying to find if the $url contains a string in $arr? Is that it? Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted May 27, 2009 Author Share Posted May 27, 2009 yes Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 27, 2009 Share Posted May 27, 2009 <?php $url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; $arr = array("dog", "cat"); $find = -1; foreach($arr as $key => $val) { $pos = strpos($url, $val); if ($pos !== false) $find = $pos; } ?> <p><?php echo $find > -1? $arr[$pos] : 'not found'; ?></p> Like that? Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted May 27, 2009 Author Share Posted May 27, 2009 wow that worked perfectly lol... can you explain your code versus mine? Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted May 27, 2009 Author Share Posted May 27, 2009 your code works on one page but not on a another so if I have the url of http://localhost/cat/page.php (and then evaluate the url using the code it does not work) but if i have http://localhost/dog/page.php (it works) so its like its only finding the first variable in the array Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted May 27, 2009 Author Share Posted May 27, 2009 ok so I have changed the code up a little bit $url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; $arr = array("dog", "cat"); $find = count($arr); for($i > 0; $i > $find; $i++) { $find = strstr($url, $arr[$i]); } echo '<p>' . $find . '</p>'; it echoes out the actual numbered position of the value in the array (EX: cat equates to position 2; dog equates to position 1) Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted May 27, 2009 Author Share Posted May 27, 2009 ok so I am completely lost and am racking the hell out of my brain... any suggestions? Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted May 27, 2009 Author Share Posted May 27, 2009 changed my code yet again: $url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; $arr = array("dog", "cat"); $explode = explode("/", $url); foreach($arr as $val){ if(in_array($val, $explode)) { $find = $val; } else { $find = "NOPE!"; } } if in the url cat is found and cat happens to be last in the array then it works... but if cat is in the url but is first in the array then it does not work Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.