URL 路由正则表达式帮助
我正在尝试为我的新 CMS 创建一个 url 路由脚本,但必须承认正则表达式不是我的强项。到目前为止,我一直遇到错误或没有结果。
我想要完成的是使用某些标签,例如 :id
:year
:slug
等...
任何人都可以帮助我或指导我的方向是正确的,那就是如何使用 preg_match 或类似的函数来找到正确的“url 模式”? Google 还没有做过一次这样的工作:S
添加
示例网址 http://www.mysite.com/post/2011/08/15/title-of-a-blogg- post/
如果我有一个路线数据库,并且一种模式是例如 post/:year/:month/:day/:slug
我希望它匹配此模式并调用某个控制器,动作,在这个例子中是某个 文章。
我创建的正则表达式数组看起来像
$patterns = array(
":id" => "/^[0-9]*$/",
":year" => "/^([0-9]{4})*$/",
":year_short" => "/^([0-9]{2})*$/",
":month" => "/^([0-9]{2})*$/",
":day" => "/^([0-9]{2})*$/",
":slug" => "/^[a-zA-Z0-9 -]*$"
);
我认为我需要将 :id
替换为 /^[0-9]*$/
然后运行 preg_match 来查找 url模式存在于我的路线表中。但是我不知道我是否使用了正确的正则表达式模式或者完全迷失了。
我的 .htaccess
文件是(因为我也需要使用 $_GET),
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
我基本上用它来获取 url 并忽略 $_GET 变量。
$route_orginal = trim($_SERVER["REQUEST_URI"]);
if(strpos($route_orginal, "?")!=FALSE) {
list($route_orginal, $get_orginal) = explode("?", trim($_SERVER["REQUEST_URI"]));
}
if( substr($route_orginal,(strlen($route_orginal)-1),strlen($route_orginal)) == "/") {
$this->routes = substr($route_orginal,1,(strlen($route_orginal)-2));
} else {
$this->routes = substr($route_orginal,1,strlen($route_orginal));
}
I'm trying to create a url routing script for my new CMS but must confess that regex isn't my strong side. So far i keep running into errors or no results.
What i'm trying to accomplish is using certain tags like :id
:year
:slug
etc...
Can anybody help me out or guide me to the right direction with this, that is how to use preg_match or similar functions to find the right "url pattern"? Google has not been doing it job for once :S
ADDED
Example url http://www.mysite.com/post/2011/08/15/title-of-a-blogg-post/
If i have a route database and one pattern is for example post/:year/:month/:day/:slug
i want it to match this pattern and call a certain controller, action and in this example a certain article.
The regex array i created looks like
$patterns = array(
":id" => "/^[0-9]*$/",
":year" => "/^([0-9]{4})*$/",
":year_short" => "/^([0-9]{2})*$/",
":month" => "/^([0-9]{2})*$/",
":day" => "/^([0-9]{2})*$/",
":slug" => "/^[a-zA-Z0-9 -]*$"
);
I reckon i need to replace :id
to /^[0-9]*$/
and afterwards run a preg_match to find if the url pattern exists in my routes table. However i don't know if i'm using the right regex patterns or just completely lost.
My .htaccess
file is (because i need to use $_GET as well)
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
I use this basically to fetch the url and leave out $_GET variables.
$route_orginal = trim($_SERVER["REQUEST_URI"]);
if(strpos($route_orginal, "?")!=FALSE) {
list($route_orginal, $get_orginal) = explode("?", trim($_SERVER["REQUEST_URI"]));
}
if( substr($route_orginal,(strlen($route_orginal)-1),strlen($route_orginal)) == "/") {
$this->routes = substr($route_orginal,1,(strlen($route_orginal)-2));
} else {
$this->routes = substr($route_orginal,1,strlen($route_orginal));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定你想实现什么目标。
但是,如果您想要一个像 www.mysite.com/tags/id-year-slug 这样的 URL
,其中每个标签都用连字符分隔,您可以这样做:
首先,您的根目录中需要一个 .htaccess 文件来创建漂亮的 url 。
然后在索引中,通过 - 分隔符分解标签:
现在您有了一个标签数组,可以将其用于 sql,并且 url 非常高 - 高 5!
I'm not sure what you are trying to accomplish.
But if you want a URL like www.mysite.com/tags/id-year-slug
where every tag is seperated by a hyphen, you could do like this:
First, you need a .htaccess file in your root to create pretty urls.
Then in your index, you explode the tags by the - delimiter:
Now you have an array of tags, which you can use for your sql and the url is pretty - high five!