[英]Mysql query to check if url exists in database
lets say I have $_POST['url']=' http://myexamplesite.com/images/image.jpg '; 假设我有$ _POST ['url'] =' http://myexamplesite.com/images/image.jpg '; how can I get correct result if this url exists in the database but looks like: http://www.myexamplesite.com/images/image.jpg
如果此URL存在于数据库中,我怎样才能获得正确的结果,但如下所示: http : //www.myexamplesite.com/images/image.jpg
if I do: 如果我做:
$q='select id from products where url LIKE "%'.$_POST['url'].'%"';
This will not return always correct results. 这将不会返回始终正确的结果。 What is the correct way?
什么是正确的方法? Thanks
谢谢
If you need pure SQL solution, you can use SUBSTRING_INDEX()
for that: 如果您需要纯SQL解决方案,可以使用
SUBSTRING_INDEX()
:
SELECT * FROM products WHERE
SUBSTRING_INDEX(SUBSTRING_INDEX(url, 'http://', -1), 'www.',-1) =
SUBSTRING_INDEX(SUBSTRING_INDEX('".$_POST['url']."', 'http://', -1), 'www.',-1)
This will remove http://
and www.
这将删除
http://
和www.
from both strings and will compare the rest. 从两个字符串,将比较其余的。
Either 或
1) Standardise the URLs within the database if they are all on your own site 1)标准化数据库中的URL(如果它们都在您自己的站点上)
or 要么
2) Remove the http://, http://, and www. 2) 删除http://,http://和www。 from search URL like so:
来自搜索网址,如下所示:
function pure_url($url) {
if ( substr($url, 0, 7) == 'http://' ) {
$url = substr($url, 7);
}
if ( substr($url, 0, 8) == 'https://' ) {
$url = substr($url, 8);
}
if ( substr($url, 0, 6) == 'ftp://') {
$url = substr($url, 6);
}
if ( substr($url, 0, 4) == 'www.') {
$url = substr($url, 4);
}
return $url;
}
$url = pure_url('http://www.myexamplesite.com/images/image.jpg');
// $url = 'myexamplesite.com/images/image.jpg';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.