I need to extract a piece of a URL in PHP. From the URL http://www.flickr.com/photos/28370443#N08/7885410582 I need only the 7885410582 part.
My code:
$string2 = 'http://www.flickr.com/photos/28370443#N08/7885410582';
preg_match_all('~s/ (.*?) /~i',$string2,$matches, PREG_PATTERN_ORDER);
echo $matches[1][0] . '<br />';
Can anyone look at it and correct it for me?
Thanks in advance.
you can use
$data = explode("/",$url)
$value = $data[sizeof($data)-1]
try this regex:
\d+$
will find all digits before string end
or:
(?<=/)\d+$
will find all digits after / sign before string end
If the identifier you want is always the last in the URL, don't bother with regular expressions and instead use simple string functions.
<?php
$url = 'http://www.flickr.com/photos/28370443#N08/7885410582';
$flickr_id = substr($url, strrpos($url, "/") + 1);
echo $flickr_id; // 7885410582
Try this:
preg_match_all('~^.+/(\d+)$~',$string2,$matches);
Description
Demo
http://regex101.com/r/aL2hL3
Sample code
$string2 = 'http://www.flickr.com/photos/28370443#N08/7885410582';
preg_match('~^.+/(\d+)$~',$string2,$matches);
echo $matches[1]; // 7885410582
Related
I have a URL string like
http://mydomain.com/status/statusPages/state/stack:3/my_link_id:1#state-9
I want to remove my_link_id:1 from this string. I know I can use any string replace function like
$goodUrl = str_replace('my_link_id:1', '', $badUrl);
But the problem is, the integer part is dynamic. I mean in my_link_id:1 the 1 is dynamic. It is the ID of the link and it can be from 0 to any number. So I want to remove my_link_id:along with any dynamic number from the string.
What I think I should remove part of the string from last / to #. But how can I do that?
you can use regular expressions:
$goodUrl = preg_replace('/(my_link_id:[0-9]+)/ig', '', $badUrl);
You can use preg_replace() php function
http://in1.php.net/preg_replace
CODE:
<?php $string = "http://mydomain.com/status/statusPages/state/stack:3/my_link_id:1#state-9";
echo $result = preg_replace('/(my_link_id:[0-9]+)/si', '', $string);
?>
Output :
http://mydomain.com/status/statusPages/state/stack:3/#state-9
May following help you
$strUrl = "http://mydomain.com/status/statusPages/state/stack:3/my_link_id:1#state-9";
$finalUrl = preg_replace('/(my_link_id:[0-9]+)/i', '', $strUrl);
echo $finalUrl;
and If you want remove also last / and # you follow this code
$strUrl = "http://mydomain.com/status/statusPages/state/stack:3/my_link_id:1#state-9";
$finalUrl = preg_replace('/(\/my_link_id:[0-9]+#)/i', '', $strUrl);
echo $finalUrl;
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);
This is just a really simple regex-question. I'd like to grab the last numbers from a url that looks like this:
http://artige.no/bilde/6908
The url will always look like this, with a number after /bilde/
You do not really need a regex for this.
$url = 'http://artige.no/bilde/6908';
$num = intval(substr($url, strrpos($url, '/') + 1));
echo $num;
In which case,
~/bilde/(\d+)~
Is what you seek. Will find any number of digits after the string /bilde/
$matches = array();
$url = "http://artige.no/bilde/6908";
preg_match('/([0-9]+)$/', $url, $matches);
print_r($matches);
This uses the pattern ([0-9]+)$ to match one or more digits at the end of the string.
"(?<=/bilde/)\d+$"
test with grep:
kent$ echo "http://artige.no/bilde/6908"|grep -Po "(?<=/bilde/)\d+$"
6908
as you required, (/bilde/) url like:
http://artige.no/NoTWithBilde/1234
will not be matched.
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];