2013-03-26 132 views
1

我目前正在研究一个项目,在这个项目中我需要这个来操纵参数以达到审美目的。使用正则表达式在URL中进行匹配

add_rewrite_rule('([^/]*)/user/([^/]+)','index.php?pagename=$matches[1]&username=$matches[2]', 'top'); 
-> would correctly retrieve the userID (in this case 232) from the following URL: http://mysite.com/userlisting/user/232 

add_rewrite_rule('([^/]*)/([^/]*)/user/([^/]+)','index.php?pagename=$matches[1]&username=$matches[2]', 'top'); 
-> would correctly retrieve the userID (in this case 232) from the following URL: http://mysite.com/parent_directory/userlisting/user/232 

现在我可以补充,让我们说的这些规则10,以确保我获得至少10个级别层次进行,但我希望做的是让它有些动态的基础上,多少级目前的“用户名单”已经。

任何帮助将不胜感激。

回答

0
add_rewrite_rule('([^/]*)/([^/]*)/user/([^/]+)','index.php?pagename=$matches[1]&username=$matches[2]', 'top'); 
-> would correctly retrieve the userID (in this case 232) from the following URL: http://mysite.com/parent_directory/userlisting/user/232 

没有,它不会。

  • $匹配[1]是parent_directory
  • $比赛[2] userlisting
  • $比赛[3]将232(即用户ID)

所以,你的内部URL会be index.php?pagename = parent_directory & username = userlisting


至于actully,解决问题,取决于你要如何处理多张水平,iehow他们会被传递给PHP,一个方法可行将

add_rewrite_rule('(.+?)/user/([^/]+)','index.php?pagename=$matches[1]&username=$matches[2]', 'top'); 

在这种情况下会给

指数.php?pagename = parent_directory/userlisting & username = 232

即,尽可能多的目录与url中的目录一样会使其成为“pagename” - 您不会排除目录名称的一部分。

但是,如果您希望不同的层次结构级别以不同的方式呈现给PHP,则可以在不同的地方完成。

+0

谢谢!它像一个魅力一样工作! – 2013-03-26 14:55:26