2017-11-11 131 views
-1

我對如何在PHP中使用URL解碼和編碼略有困惑,我需要對此進行一些澄清。URL解碼和編碼在PHP?

基本上,我有一個其中有單引號像這樣的MySQL數據:

This is the third\'s title that goes here!! 

'是單引號的HTML代碼。

現在我使用的URL像這樣:

title.php?t=This is the third's title that goes here!! 

而在我的PHP代碼,我嘗試使用它,像這樣:

$title = $_GET['t']; 

$urlsafe_title = urlencode($title); 
$url = $urlsafe_title; 
$htmlsafe_url = htmlspecialchars($url, ENT_QUOTES | ENT_HTML5); 


$title = utf8_decode(urldecode($htmlsafe_url)); 

但是當我嘗試使用變量$標題在MYSQL數據庫中搜索,我根本沒有結果。但是,當我從標題和MYSQL數據庫中刪除單引號時,一切正常。

有人可以請關於這個問題的建議嗎?

在此先感謝。

回答

1

我認爲你應該使用URL slugs,即像「Hello World」這樣的輸入可以作爲「hello-world」發送。所以這裏有一個功能,可以幫助你:

function gen_slug($url) 
{ 
    //prepare string with basic normalization 
    $url = strtolower($url); 
    $url = strip_tags($url); 
    $url = stripslashes($url); 
    $url = html_entity_decode($url); 
    //Remove any quotes 
    $url = str_replace('\"','',$url); 
    //Replace non-alpha chars with '-' 
    $match = '/[^a-z0-9]+/'; 
    $replace = '-'; 
    $url = preg_replace($match, $replace, $url); 
    $url = trim($url, '-'); 
    return $url; 
} 

你將不得不像title-slug或類似的東西創造你的桌子上一個額外的列,所以你可以使用它作爲一個參考。

所以你的情況,也可能是這樣的:title.php?t=this-is-my-title

希望它可以幫助你! :)

+1

haha​​haaa ...我只是在創造像這樣的笑聲。我想這就是要走的路。 –

+0

哈哈,真好! @DavidHope – MegaColorBoy