I'm trying to extract id, which is a whole number from a url, for example:
http://example.com/email/verify/106/8be57f01ac84747886acd7ae88c888112135fc7a
I'd like to extract only 106 from the string in PHP. The only dynamic variables in the URL would be the domain and the hash after 106/ The domain, id, and hash after 106/ are all dynamic.
I've tried preg_match_all('!\d+!', $url, $result), but it matches all number in string.
Any tips?
The pattern \b\d+\b might be specific enough here:
$url = "http://example.com/email/verify/106/8be57f01ac84747886acd7ae88c888112135fc7a";
preg_match_all('/\b\d+\b/', $url, $matches);
print_r($matches[0]);
This prints:
Array
(
[0] => 106
)
Try this. This will works.
$url = $_SERVER['REQUEST_URI']; //"http://example.com/email/verify/106/8be57f01ac84747886acd7ae88c888112135fc7a";
$str = explode('/',$url);
echo "<pre>";
print_r($str);
$id = $str[5]; // 106
Related
Extract the value of the u2 parameter from this URL using a regular expression. http://www.example.com?u1=US&u2=HA853&u3=HPA
<?php
$subject="http://www.example.com?u1=US&u2=HA853&u3=HPA"; //my url
$pattern='/u2=[0-9A-Za-z]*/'; //R.E that url value is only digit/Alphabet
preg_match($pattern,$subject,$match);
print_r($match[0]);
?>
Output:-
u2=HA853
How can i retrieve only HA853?
The 0 group is everything that the regex matched so either use \K to ignore the previous matches of the regex,
$subject="http://www.example.com?u1=US&u2=HA853&u3=HPA"; //my url
$pattern='/u2=\K[0-9A-Za-z]*/'; //R.E that url value is only digit/Alphabet
preg_match($pattern,$subject,$match);
print_r($match[0]);
or use a second capture group:
...
$pattern='/u2=([0-9A-Za-z]*)/'; //R.E that url value is only digit/Alphabet
...
print_r($match[1]);
Why you'd need to do that though is unclear to me, http://php.net/manual/en/function.parse-str.php, seems like a simpler approach.
$subject="http://www.example.com?u1=US&u2=HA853&u3=HPA";
parse_str($subject, $output);
echo $output['u2'];
Demo: https://3v4l.org/gR4cb
Other way is to use parse_url,http://php.net/manual/en/function.parse-url.php
$subject="http://www.example.com?u1=US&u2=HA853&u3=HPA";
$query_string = parse_url($subject, PHP_URL_QUERY); // get query string
$parameters = explode('&', $query_string); //Explode with &
$array = array(); // define an empty array
foreach($parameters as $val)
{
$param= explode('=', $val);
$array[$param[0]] = $param[1];
}
echo $array['u2']; // outputs HA853
print_r($array);
Array
(
[u1] => US
[u2] => HA853
[u3] => HPA
)
how can I use php to get exactly the id from google play url.
Example:
Google Play Url: https://play.google.com/store/apps/details?id=com.zing.zalo&hl=en
I want to get the com.zing.zalo . Thank you!
Simple Way
use preg_match or get the id from the url using $_GET['id'], if
you get this from url as other answer did.
$Url = "https://play.google.com/store/apps/details?id=com.zing.zalo&hl=en";
preg_match("/[^?]+(?:\?id=([^&]+).*)?/", "$Url", $matches);
echo $matches[1]; //com.zing.zalo
Working Example here Check online
The Longest way:
Simply you can use some PHP function to get it. Lets you have the following url.
$Url = "https://play.google.com/store/apps/details?id=com.zing.zalo&hl=en";
so what you need to explode the url using ? which is only one on the url.
$arr = explode("?", $Url);
From that array you need to store only the second part cause you need query string. So take only $arr[1]. Now explode again the $arr[1] with the & sign which is divide the rest of the url i mean $arr[1].
$arr2 = explode("&", $arr[1]);
Now you are all set, use another explode function to get the com.zing.zalo from the $arr2[0].
$idval = explode("=", $arr2[0]);
Result, Just echo the second part of the $idval array.
echo $idval[1]; //com.zing.zalo
Use $_GET['id'] to get the query string value of id
<?php
echo $_GET['id'];
?>
You could do this with regex: (?:\?id=)(.*)\b (I'm sure there's a more effective regex for this, but this accomplishes what you require)
preg_match('/(?:\?id=)(.*)\b/', 'https://play.google.com/store/apps/details?id=com.zing.zalo&hl=en', $matches);
print_r($matches);
Returns:
Array
(
[0] => ?id=com.zing.zalo&
[1] => com.zing.zalo
)
I want to selext url which user posted. So I want to use preg_match.
String:
#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=350000
128/prog_index.m3u8?key=49bfee85b05d117a2906368428094e94
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=750000
500/prog_index.m3u8?key=49bfee85b05d117a2906368428094e94
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1000000
750/prog_index.m3u8?key=49bfee85b05d117a2906368428094e94
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1250000
1000/prog_index.m3u8?key=49bfee85b05d117a2906368428094e94
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1750000
1500/prog_index.m3u8?key=49bfee85b05d117a2906368428094e94
My PHP code:
<?php
$quality = $_POST['quality'];
$url = ''.$serviceUrl.'/'.$path.'';
$url = file_get_contents($url);
preg_match('/'.$quality.'\/prog_index.m3u8/', $url, $C);
print_r($C);
?>
Output:
Array
(
[0] => 1500/prog_index.m3u8
)
But I want like this: 1500/prog_index.m3u8?key=49bfee85b05d117a2906368428094e94
Only based on your provided sample input, add \S+ which means to pick non-whitespace characters.
preg_match('/'.$quality.'\/prog_index.m3u8\S+/', $url, $C);
^^
I have a form and one of the input names has numbers that correspond to information in my database. I need to extract the numbers and set them as variables so I can use them to store and retrieve data from the database.
Example: rdobtn_1_15 or qtybx_9_82
then numbers will be dynamic and will change so I need something that will get the numbers whether they are "20" or "5327"
$input = 'rdobtn_1_15'
$matches = null;
$returnValue = preg_match('/_(\\d+)_(\\d+)/', $input, $matches);
Your matches will be stored as
array (
0 => '_1_15',
1 => '1',
2 => '15',
)
So your numbers are accessable via
echo $matches[1] // 1
echo $matches[2] // 15
For reference: Regular Expressions
You can extract the digits from a string, if this is what you want to achieve, with:
preg_match_all('!\d+!', $your_string, $matches);
print_r($matches);
$array = explode('_', $string);
$element = $array[5]; // or whichever element you want
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
extract last part of a url
I have a url like this:
http://www.domain.co.uk/product/SportingGoods/Cookware/1/B000YEU9NA/Coleman-Family-Cookset
I want extract just the product name off the end "Coleman-Family-Cookset"
When I use parse_url and print_r I end up with the following:
Array (
[scheme] => http
[host] => www.domain.co.uk
[path] => /product/SportingGoods/Cookware/1/B000YEU9NA/Coleman-Family-Cookset
)
How do I then trim "Coleman-Family-Cookset" off the end?
Thanks in advance
All the answers above works but all use unnecessary arrays and regular expressions, you need a position of last / which you can get with strrpos() and than you can extract string with substr():
substr( $url, 0, strrpos( $url, '/'));
You'll maybe have to add +/- 1 after strrpos()
This is much more effective solution than using preg_* or explode, all work though.
$url = 'http://www.domain.co.uk/product/SportingGoods/Cookware/1/B000YEU9NA/Coleman-Family-Cookset';
$url = explode('/', $url);
$last = array_pop($url);
echo $last;
You have the path variable(from the array as shown above).
Use the following:
$tobestripped=$<the array name>['path']; //<<-the entire path that is
$exploded=explode("/", $tobestripped);
$lastpart=array_pop($exploded);
Hope this helps.
$url = rtrim($url, '/');
preg_match('/([^\/]*)$/', $url, $match);
var_dump($match);
Test