开发者

How to create PCRE-pattern that matches only when there is 2 character

I'm currently trying to create SEO-friendly URL's. Because of that I have created the following regular expression. What I want is to lead to another URL when the URL is e.g.:

da/something/something-else.html => 'da/pages/view/something/somet开发者_如何学运维hing-else'
something/something-else.html => 'pages/view/something/something-else'
something => 'pages/view/something'

I have created this regular expression:

'/(([a-z][^\/])(1))(?:(\/))([^.]+)\.html/' => '$1$2pages/view/$3/'

What is needed is, that the first part HAS TO BE 2 CHARACTER LONG - if it is not, then it isn't a language-tag and shouldn't be passed. Then the rest just has to NOT have . (dot) inside (because of the ".html" part).

Is this possible??


Have a try with :

$arr = array('da/something/something-else.html', 'something/something-else.html', 'something');
foreach ($arr as $str) {
    echo $str,' => ';
    $str = preg_replace("~^([a-z]{2}/|)([^.]*)(?:\.html)?$~", "$1pages/view/$2", $str);
    echo $str,"\n";
}

output:

da/something/something-else.html => da/pages/view/something/something-else
something/something-else.html => pages/view/something/something-else
something => pages/view/something

explanation could be viewed here


I didn't tested it but you can add the restriction to your expression this way:

'#^/[a-z]{2}/(([a-z][^\/])(1))(?:(\/))([^.]+)\.html#' => '$1$2pages/view/$3/'

^/[a-z]{2}/ will force to have 2 [a-z] in the beginning (^) of the line.

I have changed the delimiter from / to # so you don't need to escape the /.

ps. Language codes can have more than 2 letters if they contain the region modifier: pt_BR, pt_PT and en_US.


Assuming your subject text is always a relative URL, this should do it:

$re = '%^([A-Za-z]{2}/)?(.*?)(\.html)?%i';
$url = preg_replace($re, '$1pages/view/$2', $url);

Edit: Fixed to remove optional .html extension.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜