I have a link, something like this:
$link = 'http://www.example/data/showall.php?quantity=&lang=eng&sura=2&ayat=21';
Now I want the first number of it, In this case I want 2
.
Note: All characters are fixed except the numbers.
To simplify my string, I can replace the first of it with empty, something like this:
$link ='2&ayat=21';
Now I want 2
.
I can do that by regex: /\d+/
. But I don't want to remove it, I want it, how can I do this ?
try this to get capture first digit:
[^0-9]*(\d+)
PHP Code:
<?php
$link = 'http://www.example/data/showall.php?quantity=&lang=eng&sura=2&ayat=21';
preg_match('/[^0-9]*(\d+)/',$link,$matches);
echo $matches[1];
?>
Output:
2
In case you do not want to use REGEX,
$url = 'http://www.example/data/showall.php?quantity=&lang=eng&sura=2&ayat=21';
parse_str( parse_url( $url, PHP_URL_QUERY), $array );
$first = array_filter($array, function($v){
return is_numeric($v);
});
var_dump(array_values($first)[0]);
Full code from my comment
$link = 'http://www.example/data/showall.php?quantity=&lang=eng&sura=2&ayat=21';
$parse = parse_url($link);
parse_str($parse['query'], $get);
echo $get['sura']; // or var_dump($get);
/* output $get['sura'] = 2
output $get['ayat'] = 21 */