我用它来显示在网站上创建类似 http://www.mywebaddress.com/bids/display.php?id=id 页面,但我想显示的URL与页面的标题页面标题,某事像这样 http://www.mywebaddress.com/bids/title-of-the-bid/或 出价/标题 - of-the-bid.html使用的URL页/职位
在此先感谢
我用它来显示在网站上创建类似 http://www.mywebaddress.com/bids/display.php?id=id 页面,但我想显示的URL与页面的标题页面标题,某事像这样 http://www.mywebaddress.com/bids/title-of-the-bid/或 出价/标题 - of-the-bid.html使用的URL页/职位
在此先感谢
看看这个网页的网址。
你可以让你的URL像
http://www.mywebaddress.com/bids/1234/title-of-the-bid/
,其中1234的帖子ID您传递和下一参数可以帮助您建立搜索引擎友好的URL。
此外,如果你想要一个像
http://www.mywebaddress.com/bids/title-of-the-bid/
然后尝试做标题的帖子在数据库中独特的并且你可以通过该字段检索数据查询。第一种选择会更好。如果您仍然希望采用第二种方法,请在使标题唯一后使用此article执行此操作。
做同样的,你将不得不写一个url重写规则与Apache mod_rewrite
这“的帮助会是很有益的,不只是这个样本但对于任何其他清洁网址的东西。
这里是你可以用它来创建网址
/** * Create URL Title * * Takes a "title" string as input and creates a * human-friendly URL string with either a dash * or an underscore as the word separator. * * @access public * @param string the string * @param string the separator: dash, or underscore * @return string */ function urlTitle($str, $separator = 'underscore', $lowercase = TRUE) { if ($separator == 'dash') { $search = '_'; $replace = '-'; } else { $search = '-'; $replace = '_'; } $trans = array( '&\#\d+?;' => '', '&\S+?;' => '', '\s+' => $replace, '[^a-z0-9\-\._]' => '', $replace.'+' => $replace, $replace.'$' => $replace, '^'.$replace => $replace, '\.+$' => '' ); $str = strip_tags($str); foreach ($trans as $key => $val) { $str = preg_replace("#".$key."#i", $val, $str); } if ($lowercase === TRUE) { $str = strtolower($str); } return trim(rtrim(stripslashes($str),$replace)); }
这是非常相似的笨框架中的URL辅助函数中使用的代码的功能。
如何使用?
$title = "Nice way to put it"; echo urlTitle($title);//output: "nice_way_to_put_it"
定制的其余部分是由你,但肯定这是一个良好的开端
谢谢,但我想用 这个ID http://www.mywebaddress.com/bids/1234/title-of-the-bid/这是1234. 那么,怎么样我可以分裂它从网址获取id? – user1814210
这只是将id附加到格式化的URL字符串的问题。 $url=$id . urlTitle($title);
并使用$id=preg_replace("@^/?(\d+)/.*[email protected]",'$1',$url);
从网址获取ID –
shawndreck
@asprin我需要一个!大声笑。用户 - 你想看看.htaccess和漂亮的网址。谷歌是你的朋友,但这是一个开始:http://m.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/ – MrTechie
@MrTechie需要一个? – asprin
asprin asprin ... :) – MrTechie