我有这样的 htacess 规则:
RewriteRule ^([A-z])([0-9]+)-([^/]*)?$ index.php?tt=$1&ii=$2&ll=$3
是否有任何 PHP 函数可以做到这一点?
就像是:
$A = XXXX_preg_match("([A-z])([0-9]+)-([^/]*)" , "A123-boooooo");
// $A become to =array("A","123","boooooo")
如果您只想检索这三个值,您可以传递一个输出参数,preg_match
如下所示:
preg_match(
'~^([A-z])([0-9]+)-([^/]*)$~' ,
'A123-boooooo',
$matches
);
$fullMatch = $matches[0]; // 'A123-boooooo'
$letter = $matches[1]; // 'A'
$number = $matches[2]; // '123'
$word = $matches[3]; // 'boooooo'
// Therefore
$A = array_slice($matches, 1);
如果您想立即进行更换,请使用preg_replace
:
$newString = preg_replace(
'~^([A-z])([0-9]+)-([^/]*)$~',
'index.php?tt=$1&ii=$2&ll=$3',
'A123-boooooo
);
这些文档通常非常适合提供更多信息。
preg_match('/([a-zA-Z])(\d+)-([^\/]+)/', 'A123-boooooo', $A);
array_shift($A);
输出: print_r($A);
Array
(
[0] => A
[1] => 123
[2] => boooooo
)
根据preg_match文档
preg_match("~([A-z])([0-9]+)-([^/]*)~" , "A123-boooooo", $matches);
print_r($matches);
输出:
Array
(
[0] => A123-boooooo
[1] => A
[2] => 123
[3] => boooooo
)