regex part of url - php

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];

Related

Retrieve 'id' from an URL using regular expression

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];
?>

isolate the number exists at the end of the URI php

I have the following URI:
/belt/belts/fk/product/40P35871
And I want to retrieve the last content after the last /.
In this case is 40P35871.
How can I do this?
How about explode?
$elements = explode('/', $input);
$productId = end($elements);
Here's a different solution entirely. (and the simplest!)
Using basename
$var = "/belt/belts/fk/product/40P35871";
echo basename($var);
Output:
40P35871
You don't need regex for something simple like that. Consider using strrchr, documentation here
$lastcontent = substr(strrchr($uri, "/"), 1);
Considering this special case of $uri being a path, the best answer would be the one provided by Chtulhu.
basename will return the last part of a path, documentation here
$lastcontent = basename($uri);
Just like this
$str = '/belt/belts/fk/product/40P35871';
$arr = explode('/', $str);
$var = array_pop($arr);
var_dump($var);
or
$var = substr($str, strrpos($str,'/') + 1);
Try this
$result = preg_replace('%(/(?:[^/]+?/)+)([^/]+)\b%', '$2', $subject);
use this:
echo preg_replace('/[a-z0-9]$/i', '$1', $url);
this will give you the last position
note: but on this url only, query strings make this useless and use need to parse the url for the same first for this to work
Don't use regex. In this case you can act as the follow
myUrl = $_SERVER[REQUEST_URL];
$number = substr(strrpos(myUri,'/')+1);
You don't need regex.
Find the last content and get it using substr():
$lastcontent = substr(strrchr($uri, "/"), 1);

How to get a string from url using preg_match?

I want to extract id from an url using php preg_match..
For eg: $string = 'http://www.mysite.com/profile.php?id=1111'; I need output as '1111'
using preg_match.
I tried this :
if(preg_match('/(?:https?):\/\/www\.mysite\.com\/profile\.php\?id\=[0-9]/', $string, $match) > 0){
$id = $match[1];
}
But am getting output as 'profile.php'
Can someone help me please?
Why not use parse_url with parse_str
$url_component = parse_url($string);
parse_str($url_component['query'], $output);
echo $output['id'];
if there is only one parameter in the url you can use explode function to do that easily, like
$string = 'http://www.mysite.com/profile.php?id=1111';
$ex=explode('=',$string);
$id=$ex[1];
You may also use parse_url function of php.
Hope this help,
$pattern = '/(?:https?):\/\/www\.mysite\.com\/profile\.php\?id\=([0-9]+)/';
This should work fine!
But do this with parse_url

Use preg_match to grab value from URL

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

Regex to get ID of pastebin.com

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);

Categories