-1

I use the code below to check if the URL contains a specific word (eg. foobar):

<?php $geturl = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; ?>

<?php if (strpos($geturl, 'foobar') == true) {   
//Do something here
} ?>

This works pretty much perfect but if i try to search for a Greek word (eg. καλημέρα)

<?php if (strpos($geturl, 'καλημέρα') == true) {   
//Do something here
} ?>

then it doesn't work. I tried to use also mb_strpos and didn't work too. How can i make it work ?

UPDATE: If i try to echo the saved url with <?php echo $geturl; ?> i am getting this:

/myserver/%CE%BA%CE%B1%CE%BB%CE%B7%CE%BC%CE%AD%CF%81%CE%B1 

Instead it should be like that:

/myserver/καλημέρα
Designer
  • 875
  • 7
  • 26
  • Can you provide string example with greek part, I mean `$geturl` with greek in it, because there is no errors in your code? – MorganFreeFarm Dec 09 '19 at 14:34
  • I have no idea where this "%CE.." comes from. With urldecode("/myserver/%CE%BA%CE%B1%CE%BB%CE%B7%CE%BC%CE%AD%CF%81%CE%B1") you get "/myserver/καλημέρα" – jspit Dec 10 '19 at 09:11
  • You need to use `urldecode($geturl)` to convert the urlencoded URL to its corresponding characters and then you should use `mb_strpos`. Remeber, greek characters themselves are not valid in a URL that's why they are URL encoded when you receive them – apokryfos Dec 10 '19 at 09:28
  • thanks @apokryfos i will try your suggestion. – Designer Dec 10 '19 at 09:40
  • 1
    mb_strpos () only needs to be used if the position of the character is to be determined. To check whether the string is contained strpos() is sufficient. – jspit Dec 10 '19 at 11:34

2 Answers2

0

For this case apreg_match can do the job for you. You need to match anything that is not in a default character set. In this case I assume that the greek characters are not inside ASCII.

According to this answer by Karolis, you could try to use the following:

if(preg_match('/[^\x20-\x7e]/', $string))
Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
gi097
  • 7,313
  • 3
  • 27
  • 49
-1

That is not correct

if (strpos($geturl, 'foobar') == true) ..

You must use

if( strpos($geturl, 'καλημέρα') !== false){
  //do
}

If the word you are looking for is somewhere in the string, strpos returns a numeric value. It returns false if nothing is found. It never returns true.

Update: Use for $geturl that

$geturl = 'http://' . urldecode($_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
jspit
  • 7,276
  • 1
  • 9
  • 17
  • `== true` will pass for any number greater than 0 so technically in this case it would work (it would only not work if the needle is found at the beginning of the haystack) – apokryfos Dec 10 '19 at 09:31
  • Even if it works for the special case here, this remains unclean. – jspit Dec 10 '19 at 12:16