I have the following url : http://example.com/category/post-is-783476?src=mainPage
. With the pattern How to get the numbers(78346) from the url ?
Asked
Active
Viewed 289 times
-2

Touheed Khan
- 2,149
- 16
- 26

Jahid
- 77
- 1
- 9
-
Possible duplicate of [Get number from an url in PHP](https://stackoverflow.com/questions/23886584/get-number-from-an-url-in-php) – Florian Humblot May 29 '17 at 10:49
-
Try with `/(\d+)/g` – prasanth May 29 '17 at 10:55
-
1Possible duplicate of [what is the easiest way of getting a number from a url?](https://stackoverflow.com/questions/5696628/what-is-the-easiest-way-of-getting-a-number-from-a-url) – mickmackusa May 29 '17 at 12:47
1 Answers
0
Something like
<?php
$input_line = "http://example.com/category/post-is-783476?src=mainPage";
preg_match("/([0-9]+)\/?/", $input_line, $output_array);
echo $output_array[1];
?>
Output: 783476

Pieter De Clercq
- 1,951
- 1
- 17
- 29
-
-
-
I mean The Url is different for every url like http://example.com/category/hello-world-673534?src=mainPage, I just want the numbers from url dynamically. – Jahid May 29 '17 at 11:10
-
-
-
Without regex you can also do that: `$result = array_pop(explode('-' ,parse_url($input_line, PHP_URL_PATH)));` – Casimir et Hippolyte May 29 '17 at 20:22