I need to extract only the 'id' from an URL. The url format is given below. Currently, I use parse_url and substr function, which works. However, it is not a good choice if the length of 'id' changes, where regex comes into play. I am not well-conversant with regex, so I need an idea how to do it in other way e.g. regex.
$url = 'http://www.example.com/stock-footage/53833534/portrait-lifestyle-leisure-caucasian-parents-children-snow-v.html';
$URLParts = parse_url($url);
// echo $URLParts['path'];
$substring = substr($URLParts['path'],15,8);
echo $substring;
You can use the following regular expression:
preg_match("/.+\/([0-9]+).+/", $url, $matches);
echo $matches[1];
live example
you can use this :
<?php
$url = 'http://www.example.com/stock-footage/53833534/portrait-lifestyle-leisure-caucasian-parents-children-snow-v.html';
$URLParts = parse_url($url);
$exploded = explode('/',$URLParts['path']);
echo $exploded[2];
?>
Related
I want to ouput only MYID from URL. What I did so far:
$url = "https://whatever.expamle.com/display/MYID?out=1234567890?Browser=0?OS=1";
echo substr($url, 0, strpos($url, "?out="));
output: https://whatever.expamle.com/display/MYID
$url = preg_replace('#^https?://whatever.expamle.com/display/#', '', $url);
echo $url;
ouput: MYID?out=1234567890?Browser=0?OS=1
How can I combine this? Thanks.
For a more general solution, we can use regex with preg_match_all:
$url = "https://whatever.expamle.com/display/MYID?out=1234567890?Browser=0?OS=1";
preg_match_all("/\/([^\/]+?)\?/", $url, $matches);
print_r($matches[1][0]); // MYID
When the string is always a Uniform Resource Locator (URL), like you present it in your question,
given the following string:
$url = "https://whatever.expamle.com/display/MYID?out=1234567890?Browser=0?OS=1";
you can benefit from parsing it first:
$parts = parse_url($url);
and then making use of the fact that MYID is the last path component:
$str = preg_replace(
'~^.*/(?=[^/]*$)~' /* everything but the last path component */,
'',
$parts['path']
);
echo $str, "\n"; # MYID
and then depending on your needs, you can combine with any of the other parts, for example just the last path component with the query string:
echo "$str?$parts[query]", "\n"; # MYID?out=1234567890?Browser=0?OS=1
Point in case is: If the string already represents structured data, use a dedicated parser to divide it (cut it in smaller pieces). It is then easier to come to the results you're looking for.
If you're on Linux/Unix, it is even more easy and works without a regular expression as the basename() function returns the paths' last component then (does not work on Windows):
echo basename(parse_url($url, PHP_URL_PATH)),
'?',
parse_url($url, PHP_URL_QUERY),
"\n"
;
https://php.net/parse_url
https://php.net/preg_replace
https://www.php.net/manual/en/regexp.reference.assertions.php
I want to explode urls to get the host.
$link = str_replace("www.", "", $_POST['link']);
$part1 = explode("//", $link);
$part2 = explode(".", $part1[1]);
$host = $part2[0];
So if the $_POST['link'] is e.g.
https://www.youtube.com/watch?v=udm5jUA-2bs
I want to get just youtube.
In this first way I get explode() expects parameter 2 to be string, array given" error message.
With this method:
$host = var_dump(parse_url($_POST['link'], PHP_URL_HOST));
I get www.youtube.com but I want only youtube.
Extract the url domain using parse_url() function with mode PHP_URL_HOST.
We will use explode() function to break it into parts using . as delimiter.
Now, the URL can be of two types, either youtube.com, or www.youtube.com, or %.youtube.com
We count the parts, and if they are two, we use the first value, else the second value.
DEMO: https://3v4l.org/KlErW
$url_host_parts = explode('.', parse_url($_POST['link'], PHP_URL_HOST));
$host = (count($url_host_parts) == 2) ? $url_host_parts[0] : $url_host_parts[1];
what about this without any regex? You can use parse_url() and explode() to get what you want i.e youtube
<?php
$url = 'https://www.youtube.com/watch?v=udm5jUA-2bs';
$parse = parse_url($url);
//print_r($parse);
echo explode('.',$parse['host'])[1];
?>
Output:
youtube
DEMO: https://3v4l.org/kG1nD
You can use regex in preg_match() to do this work.
$url = "https://www.youtube.com/watch?v=udm5jUA-2bs";
preg_match("/(\w+)(?=\.\w{2,}(\/|\?|$))/", $url, $matches);
echo $matches[1]; // youtube
Check result in demo
I have a url something like:
www.yourname.mysite.com/templates/diversity/index.php
I need to grab that string, "diversity", and match it against a db value. I've never used anything like preg_match in php, how can I grab that string?
Instead of a regular expression, you can use parse_url() for that:
$url = 'www.yourname.mysite.com/templates/diversity/index.php';
$parts = parse_url($url);
$paths = explode('/', $parts['path']);
// "diversity" is in $paths[1]
For completeness' sake, here's the regular expression:
preg_match('=^[^/]+/[^/]+/([^/]+)/=', $url, $matches);
// "diversity" is in $matches[1]
$ex = explode("/",$_SERVER["PHP_SELF"]);
echo $ex[2];
You can just explode that string and get the third value of the resulting array:
$parts = explode("/", "www.yourname.mysite.com/templates/diversity/index.php");
echo $parts[2]; // diversity
I need to get the ID part of a pastebin link,
which is setup like http://pastebin.com/{id}, i have tired alot of different regex i am also using preg_match in php
preg_match("~http://pastebin.com/([0-9a-zA-Z]+)~", $url, $match);
print_r($match);
or
$url = "http://pastebin.com/a65d46";
$parsed = parse_url($url);
echo trim($parsed['path'])." is ID you needed";
Instead of regex, try using parse_url to extract the path
regex would be overkill for this.
$url = "http://pastebin.com/Ugj1eqCN"
$pos = strpos($url,"pastebin.com/");
echo substr($url,$pos+13);
So I'm trying to get the id from a url for youtube..
here is the url
http://gdata.youtube.com/feeds/api/videos/kffacxfA7G4/related?v=2
then there's also - in the url too.
it wouldn't let me post another url but it's the same as above but with the id ucvkO0x-mL4
how can I grab between videos/ and /related (the id) with regex?
I tried to use txt2re.com which is what I always use, but it's not working for this case..
thanks!
No need for even regex, just a simple strpos and substr will do it. Or just use explode like this:
<?php
$url = 'http://gdata.youtube.com/feeds/api/videos/kffacxfA7G4/related?v=2';
//BY STRPOS/SUBSTR
echo substr($url, 42, strpos($url, '/related', 42) - 42);
//BY EXPLODE
$parts = explode('/', $url);
echo $parts[6];
?>
(?:.*)videos/(.*?)/related\?v=2
This is how I would do it
$url = 'http://gdata.youtube.com/feeds/api/videos/kffacxfA7G4/related?v=2';
preg_match('#.*videos/([0-9a-zA-Z_\-]{11})/related.*#', $url, $matches);
print_r($matches);
But, #shamittomar is right about strpos and substr
using regex you can do it by
$url = "http://gdata.youtube.com/feeds/api/videos/kffacxfA7G4/related?v=2";
preg_match('/videos\/(.+)\/related/',$url,$match);
$id = $match[1];