如何使用 preg_match 获取 URL 的特殊部分?
How to get special part of URL using preg_match?
我有一个像这样的url,
https://example.com/folder-name/article-name-xxx-xxx-xxx-xxx-xxx-xxx-5b5964935583202d2beff315.html#id-41
我想要做的是在 url.
中获取 5b5964935583202d2beff315 和 41
我真的很想知道怎么做,我需要帮助。非常感谢您的帮助!
$url = "https://example.com/folder-name/dien-hy-cong-luoc-story-of-yanxi-palace-5b5964935583202d2beff315.html#id-41";
preg_match("/^.+-([^.-]+)\.html#id-(\d+)/", $url, $matches);
print_r($matches);
输出:
Array
(
[0] => https://example.com/folder-name/dien-hy-cong-luoc-story-of-yanxi-palace-5b5964935583202d2beff315.html#id-41
[1] => 5b5964935583202d2beff315
[2] => 41
)
解释:
/ : regex delimiter
^ : beginning of line
.+ : 1 or more any character but newline
- : a dash
([^.-]+) : group 1, 1 or more any character that is not a dot or dash
\. : a dot
html#id- : literally
(\d+) : group 2, 1 or more digits
/
我有一个像这样的url,
https://example.com/folder-name/article-name-xxx-xxx-xxx-xxx-xxx-xxx-5b5964935583202d2beff315.html#id-41
我想要做的是在 url.
中获取 5b5964935583202d2beff315 和 41我真的很想知道怎么做,我需要帮助。非常感谢您的帮助!
$url = "https://example.com/folder-name/dien-hy-cong-luoc-story-of-yanxi-palace-5b5964935583202d2beff315.html#id-41";
preg_match("/^.+-([^.-]+)\.html#id-(\d+)/", $url, $matches);
print_r($matches);
输出:
Array
(
[0] => https://example.com/folder-name/dien-hy-cong-luoc-story-of-yanxi-palace-5b5964935583202d2beff315.html#id-41
[1] => 5b5964935583202d2beff315
[2] => 41
)
解释:
/ : regex delimiter
^ : beginning of line
.+ : 1 or more any character but newline
- : a dash
([^.-]+) : group 1, 1 or more any character that is not a dot or dash
\. : a dot
html#id- : literally
(\d+) : group 2, 1 or more digits
/