开发者

PHP hyphens problem when using a variable

开发者 https://www.devze.com 2023-01-31 15:41 出处:网络
My website is a search engine. In my .htaccess file I have: RewriteRule ^news/(.*)\\.html$ /results/news.php?name=$1

My website is a search engine.

In my .htaccess file I have:

RewriteRule ^news/(.*)\.html$ /results/news.php?name=$1

And my link structure is /news/what-is-up.html using hyphens.

When I search for /results/news.php?name=hello world I get different results than when I use /news/hello-world.html

It seems that the searc开发者_JS百科h is searching for hello-world using hypens instead of spaces, I could easily solve it using a + sign but I want to use hyphens. Does anyone know how to do it?

This is the code I use for converting the name variable into URL friendly:

function toAscii($str, $replace=array(), $delimiter='-') {
if( !empty($replace) ) {
    $str = str_replace((array)$replace, ' ', $str);
}

$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
$clean = strtolower(trim($clean, '-'));
$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);

return $clean;
}

and my search engine uses http location like this:

if ($q != '') {
   header( 'Location: http://'.$_SERVER['SERVER_NAME'].'/news/'.toAscii($q).'.html' );
   die();
}
else
{
    header('Location:http://'.$_SERVER['SERVER_NAME'].'/news/');
    die();
}

Thanks


If I have understood you correctly use str_replace to replace a hyphen with a space before submitting it to your search.

$search_term = str_replace("-", " ", $incoming);


You need to handle word splitting on your end since it's being rewritten.

What you can do is have

RewriteRule ^news/(.*).html$ /results/news.php?name=$1&rewrite=true

and if $_GET['rewrite'] is present, you do str_replace("-", " ", $incoming)

0

精彩评论

暂无评论...
验证码 换一张
取 消