2011-12-23 60 views
1

我有以下.htaccess文件添加 “ - ” 为.htaccess URL

Options +FollowSymLinks 
RewriteEngine on 
RewriteRule (.*)-pid-(.*)\.html$ product.php?n=$1&pid=$2 

,并在我的PHP文件,我有这样的代码:

<?php print $product->name; ?>-pid-<?php print $product[id]; ?>.html 

我如何可以自动添加 “ - ”到产品名称,所以它看起来不像这个“Apple iPod 32GB第1代”,而是看起来像这样“Apple-iPod-32GB-1st代”

谢谢

+0

请注意,您还不希望在URL中显示其他可能的产品名称字符。为了方便快捷:除了用破折号替换空格之外,还要考虑运行urlencode()。 – Thor 2011-12-23 12:40:46

回答

0
$seourl = strtolower(urlencode($product->name)); 

它使用+而不是 - ,但str_replace函数可能会改变,如果你坚持。

0

只需用PHP中的破折号替换空格即可。

<?php print str_replace(" ", "-", $product->name) ?>-pid-<?php print $product[id]; ?>.html 
0

使用str_replace()函数。

只需用hypen替换空格使用此功能。

str_replace(" ","-",$productname)