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
https://forums.phpfreaks.com/topic/159881-solved-strstr-help/#findComment-843218
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
https://forums.phpfreaks.com/topic/159881-solved-strstr-help/#findComment-843230
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
https://forums.phpfreaks.com/topic/159881-solved-strstr-help/#findComment-843256
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
https://forums.phpfreaks.com/topic/159881-solved-strstr-help/#findComment-843331
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/159881-solved-strstr-help/#findComment-843353
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
https://forums.phpfreaks.com/topic/159881-solved-strstr-help/#findComment-843371
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
https://forums.phpfreaks.com/topic/159881-solved-strstr-help/#findComment-843419
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.