[英]Check if url contain a specific non-Latin word (eg Greek) using php
我使用下面的代碼來檢查 URL 是否包含特定單詞(例如 foobar):
<?php $geturl = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; ?>
<?php if (strpos($geturl, 'foobar') == true) {
//Do something here
} ?>
這非常完美,但如果我嘗試搜索希臘詞(例如καλημέρα)
<?php if (strpos($geturl, 'καλημέρα') == true) {
//Do something here
} ?>
那么它不起作用。 我也嘗試使用mb_strpos
但也沒有用。 我怎樣才能讓它工作?
更新:如果我嘗試使用<?php echo $geturl; ?>
回顯保存的 url <?php echo $geturl; ?>
<?php echo $geturl; ?>
我得到這個:
/myserver/%CE%BA%CE%B1%CE%BB%CE%B7%CE%BC%CE%AD%CF%81%CE%B1
相反,它應該是這樣的:
/myserver/καλημέρα
對於這種情況, preg_match
可以為您完成這項工作。 您需要匹配任何不在默認字符集中的內容。 在這種情況下,我假設希臘字符不在 ASCII 中。
根據Karolis 的這個答案,您可以嘗試使用以下內容:
if(preg_match('/[^\x20-\x7e]/', $string))
那是不正確的
if (strpos($geturl, 'foobar') == true) ..
你必須使用
if( strpos($geturl, 'καλημέρα') !== false){
//do
}
如果您要查找的單詞在字符串中的某個位置, strpos將返回一個數值。 如果未找到任何內容,則返回 false。 它永遠不會返回 true。
更新:用於 $geturl
$geturl = 'http://' . urldecode($_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.