Jump to content

[SOLVED] strstr help


Recommended Posts

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;
}

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

<?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?

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.