用URL中的连字符替换空格

时间:2013-05-19 09:08:57

标签: php get mysqli seo isset

我正在尝试创建一个SEO友好的网站。目前网页有效,因为网址中显示的ID与product.php?id=4类似,但我希望将ID替换为product_name,但此值可以包含空格,因此URL看起来像product.php?product_name=fence%20panel,但这不起作用,因为我使用此GET变量来查找我的数据库中的产品详细信息,当它发生时,它认为它是一个单词,因此没有找到产品。所以我认为我需要的是一个变量,它添加一个连字符product.php?product_name=fence-panel,但也是一个变量,可以将此连字符更改回空格。如果我错了,请纠正我

if(isset($_GET['id'])){ 
$id = $_GET['id']; 
//Get the product if exists 
//if no product then give a message 
$sql = mysqli_query($myNewConnection, "SELECT * FROM products WHERE id='$id' LIMIT 1"); 
$productCount = mysqli_num_rows($sql); 
if ($productCount > 0) { 
    //get product details 
    while($row = mysqli_fetch_array($sql)){ 
        $product_name = $row["product_name"]; 
        $price = $row["price"]; 
        $details = $row["details"]; 
        $category = $row["category"]; 
        $date_added = strftime("%b %d, %Y", strtotime($row["date_added"])); 
        $_SESSION['product_price_array'] = $price; 
    } 
} else{ 
    echo "No Product in the system"; 
    exit(); 
} 

1 个答案:

答案 0 :(得分:2)

使用rawurlencode()rawurldecode()。有关示例,请参阅the PHP documentation。它不会添加连字符,但它会做你想要的。要添加连字符,您可以在编码后尝试执行以下操作:$str = str_replace('-', '%2D', $str);。你应该再次这样做,并在解码之前交换两个第一个参数!

相关问题