[英]How to get special part of URL using preg_match?
我有一個像這樣的網址
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
/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.