简体   繁体   English

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

[英]How to get special part of URL using preg_match?

I have an url like this one, 我有一个像这样的网址

https://example.com/folder-name/article-name-xxx-xxx-xxx-xxx-xxx-xxx-5b5964935583202d2beff315.html#id-41

What I'm trying to do is get 5b5964935583202d2beff315 and 41 in url. 我想做的是在URL中获取5b5964935583202d2beff31541

I really want to know how to do this, and I needs help. 我真的很想知道该怎么做,我需要帮助。 Your help would be greatly appreciated! 您的帮助将不胜感激!

$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);

Output: 输出:

Array
(
    [0] => https://example.com/folder-name/dien-hy-cong-luoc-story-of-yanxi-palace-5b5964935583202d2beff315.html#id-41
    [1] => 5b5964935583202d2beff315
    [2] => 41
)

Explanation: 说明:

/               : 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