0

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.

I really want to know how to do this, and I needs help. Your help would be greatly appreciated!

Mohammad
  • 21,175
  • 15
  • 55
  • 84
Simon
  • 101
  • 1
  • 1
  • 8
  • 1
    Possible duplicate of [Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?](https://stackoverflow.com/questions/940905/can-i-read-the-hash-portion-of-the-url-on-my-server-side-application-php-ruby) – Dezza Nov 09 '18 at 15:59
  • 1
    Does the server have `#id-41` when you get it? Is this a string or URL you receive? – user3783243 Nov 09 '18 at 15:59

1 Answers1

0
$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
/
Toto
  • 89,455
  • 62
  • 89
  • 125