簡體   English   中英

如何使用preg_match獲得URL的特殊部分?

[英]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中獲取5b5964935583202d2beff31541

我真的很想知道該怎么做,我需要幫助。 您的幫助將不勝感激!

$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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM