正则表达式问题
我有以下 URL 结构,需要匹配并从中获取特定 id:
/group/subgroup/id-name
简而言之,我需要翻译如下 URL:
/Blue Products/Dark Blue/5-Blue_Jelly
至:
/?pagename=Blue Products&model=5
重要: 我不需要匹配群组
,我已经有群组了。
示例代码:
<?php
foreach($cats as $cat)
$cmd->rewrite('/\/'.$cat.'\/unused\/(ID)-unused\//','/?pagename='.$cat.'&model=%ID%');
?>
编辑:
这是完整的代码:
if($groups->count()){
$names=array();
foreach($groups->rows as $row)
$names[]=preg_quote($row->group);
$names=implode('|',$names);
$regex='('.$names.')/([^/]+)/([0-9]{1,})-([^/]+)/?$';
CmsHost::cms()->rewrite_url($regex,'index.php?pagename=Products',true);
}
I have the following URL structure which I need to match to and get the particular id from:
/group/subgroup/id-name
In short, I need to translate a URL like the following:
/Blue Products/Dark Blue/5-Blue_Jelly
To:
/?pagename=Blue Products&model=5
IMPORTANT: I don't need to match group
, I already have group.
Example code:
<?php
foreach($cats as $cat)
$cmd->rewrite('/\/'.$cat.'\/unused\/(ID)-unused\//','/?pagename='.$cat.'&model=%ID%');
?>
Edit:
This is the completed code:
if($groups->count()){
$names=array();
foreach($groups->rows as $row)
$names[]=preg_quote($row->group);
$names=implode('|',$names);
$regex='('.$names.')/([^/]+)/([0-9]{1,})-([^/]+)/?
;
CmsHost::cms()->rewrite_url($regex,'index.php?pagename=Products',true);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
\d+
来匹配 d 小数。使用
#
作为外部分隔符以避免\/
的无关转义You can use
\d+
to match decimals.Use
#
as outer delimiters to avoid extraneous escaping of\/
当您可以使用简单的字符串函数时,为什么要使用正则表达式,如下所示:
Why use regex when you can use simple string functions like so:
试试这个:
希望这有效!
Try this one:
Hope this works!