简体   繁体   English

在PHP框架中使用正则表达式

[英]Using Regular Expressions in a PHP Framework

I've got a university project to create a site using a framework given to us by the teacher. 我有一个大学项目,要使用老师给我们的框架创建一个网站。 I've hosted the site at http://daniel-lucas.com so you can see what's happening. 我将该站点托管在http://daniel-lucas.com上,这样您可以看到发生了什么事。

As you can see the address ends with /?index. 如您所见,地址以/?index结尾。

In the configuration file I've just used : 在我刚刚使用的配置文件中:

new UrlMapping('^index/$', 'musiconlineapp.listeners.MainListener.handle', 'musiconlineapp/views/MainView.php')

The first parameter is the address to map. 第一个参数是要映射的地址。 The second is the controller for the page and the method to load in the controller (handle). 第二个是页面的控制器以及在控制器(句柄)中加载的方法。 The third is the view to display for the page. 第三是该页面显示的视图。

However I'm having trouble displaying categories for the site. 但是,我在显示网站类别时遇到了麻烦。 I was told to use mapping like this: 有人告诉我使用这样的映射:

new UrlMapping('^category/(?P<cat>\w+)/$', 'musiconlineapp.listeners.CategoryTitleListener.handle', 'musiconlineapp/views/CategoryTitleView.php' )

My problem is that I've not done any regex for a while and can't figure out what address I'm supposed to go to when I click on the link. 我的问题是,我有一段时间没有做任何正则表达式,并且在点击链接时无法弄清楚应该去的地址。 I've tried /?category&cat=rock and similar variations but none of them work. 我已经尝试过/?category&cat = rock和类似的变体,但是它们都不起作用。

That regex looks like it's supposed to match /?category/rock/ or the like. 该正则表达式看起来应该与/?category/rock/匹配。

^           # beginning of string
category/   # literal 'category/'
(?P<cat>    # named matching group (named 'cat')
\w+         # one or more word characters a-zA-Z0-9_
)           # end the named matching group
/           # literal '/'
$           # end of string

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM