Jump to content

Checking a string contains characters


conker87

Recommended Posts

I've got this as a function:

 

   function styleLink($colour)
    {
     if ($_SERVER['QUERY_STRING'])
      { echo $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING'] . "&style=" . $colour; }
     else
      { echo $_SERVER['PHP_SELF'] . "?style=" . $colour; }
    }

This works great, but if there's already a '?style=' it will still add another '&style=' and that's obviously not good. How would I check the $_SERVER['QUERY_STRING'] contains this?

Link to comment
https://forums.phpfreaks.com/topic/56739-checking-a-string-contains-characters/
Share on other sites

Ah, excellent works great.

 

But now I have another problem. Here is the code:

 

   function styleLink($colour)
    {
     if ($_SERVER['QUERY_STRING'])
      if (strpos($_SERVER['QUERY_STRING'], "style") === false)
       { echo $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING'] . "&style=" . $colour; } 
      else
       { echo $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING']; }
     else
      { echo $_SERVER['PHP_SELF'] . "?style=" . $colour; }
    }

At the moment, if there's already the style var there then it'll just echo the 'index.php?style=blue' (as an example), hoever if I try and click the purple, orange and green links it will just show 'index.php?style=blue' rather than 'index.php?style=colour'.

 

Would anyone know how to correct it so that it shows the right colour in the url in the style var is already there?

 

Apologies for the really terrible explanation, it's difficult to word it.

Cleaning up the code might help:

 

function styleLink($colour) {
if ($_SERVER['QUERY_STRING']) {
	if (strpos($_SERVER['QUERY_STRING'], "style") === false) {
		echo $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING'] . "&style=" . $colour;
	} 
	else {
		echo $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING'];
	}
}
else {
	echo $_SERVER['PHP_SELF'] . "?style=" . $colour;
}
}

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.