I am getting a variable from the url in php using $_GET[]. The url looks like: http://www.site.com/index.php?x=B%FCergschaften which i am comparing to values in an array: $y=array('Bauherrenhaftpflicht','Gebäudeversicherung','Büergschaften');
I understand that the variable in the url is decoded by the $_GET function but when i compare it to the array i am not getting a match.
This is the code i use to compare:
$x = $_GET['x']; foreach($y as $value){ if($value == $x){ echo "Found!"; } }
I have tried urldecode function but it doesn't work.
How do I correctly decode the variable from the url so that the special character is correctly decoded and the variable compared to the array?
Many thanks.
As posted in the comments:
$x = utf8_encode(urldecode($_GET['x']));
And also you should/could use in_array()
if (in_array($x, $y)) {
echo "Found!";
}
Because you are using special characters that are out of the default character set you should either decode the array values or encode the GET param value to UTF-8
I'm testing this locally and it is working for me:
$x = utf8_encode(urldecode($_GET['x']));
$y=array('Bauherrenhaftpflicht','Gebäudeversicherung','Büergschaften');
echo in_array($x,$y); // outputs 1 (true)
Let us know if it works for you.