我对如何在 PHP 中使用 URL 解码和编码有点困惑,我需要对此进行一些说明。
基本上,我有一个包含单引号的 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));
但是当我尝试使用该变量 $title 在 MYSQL 数据库中进行搜索时,我根本没有得到任何结果。但是当我从标题和 MYSQL 数据库中删除单引号时,一切正常。
有人可以就这个问题提出建议吗?
提前致谢。
我认为您应该使用 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
希望对你有帮助!:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句