Php 在url上使用页面/帖子的标题
我习惯于显示网站上创建的页面,如 但是我想在url上显示带有页面标题的页面,诸如此类 或 bids/title-of-the-bid.htmlPhp 在url上使用页面/帖子的标题,php,Php,我习惯于显示网站上创建的页面,如 但是我想在url上显示带有页面标题的页面,诸如此类 或 bids/title-of-the-bid.html 提前感谢查看此页面的url 您可以将您的url设置为 http://www.mywebaddress.com/bids/1234/title-of-the-bid/ 其中1234是您要传递的帖子id,下一个参数帮助您构建seo友好的URL 如果你想的话也可以 http://www.mywebaddress.com/bids/title-of-the-b
提前感谢查看此页面的url 您可以将您的url设置为
http://www.mywebaddress.com/bids/1234/title-of-the-bid/
其中1234是您要传递的帖子id,下一个参数帮助您构建seo友好的URL
如果你想的话也可以
http://www.mywebaddress.com/bids/title-of-the-bid/
然后尝试在数据库中将文章标题设置为唯一,您可以通过该字段进行查询以检索数据。第一种选择会更好。如果您仍然想采用第二种方法,请在使标题独特之后尝试使用此方法
要做到这一点,您必须在apache的帮助下编写url重写规则。这将非常有用,不仅对于本示例,而且对于任何其他干净的url内容
这里有一个可以用来创建URL的函数 /** * 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”字符串作为输入并创建 *带有破折号或破折号的人性化URL字符串 *或下划线作为单词分隔符。 * *@access-public *@param字符串字符串 *@param字符串分隔符:破折号或下划线 *@返回字符串 */ 函数urlTitle($str,$separator='下划线',$lowercase=TRUE) { 如果($separator=='dash') { $search=''"; $replace='-'; } 其他的 { $search='-'; $replace='; } $trans=数组( “&\\d+?”=>”, “&\S+?;”=>”, “\s+”=>$replace, “[^a-z0-9\-\.\”=>”, $replace.'+'=>$replace, $replace.'$'=>$replace, “^”。$replace=>$replace, '\.+$' => '' ); $str=带标签($str); foreach($trans as$key=>$val) { $str=preg_replace(“#“.”$key.#i“,$val,$str); } if($lowercase===TRUE) { $str=strtolower($str); } 返回修剪(rtrim(条纹斜线($str),$replace)); } 这与codeigniter框架中url助手函数中使用的代码非常相似 如何使用 $title=“很好的表达方式”; 头衔($头衔)//输出:“很好的表达方式”
其余的定制由您决定,但这肯定是一个良好的开端@asprin我需要一个!LOL.User-您想查看.htaccess和漂亮的URL。谷歌是你的朋友,但这里有一个开始:谢谢,但我想使用的id是1234。那么,如何拆分它以从url获取id呢?只需将id附加到格式化的url字符串中即可<代码>$url=$id。头衔($头衔)并使用
$id=preg\u replace(“@^/?(\d+/*$@“,“$1”,“$url”)
从url获取id
$title = "Nice way to put it";
echo urlTitle($title);//output: "nice_way_to_put_it"