How get last segment of url using PCRE? - php

I have a url, - "http://example.com/sales/view/id/705" and I need get a last segment (705).
How can I do this using PCRE?

This should do it in Perl:
my ($last) = $url =~ /([^\/]+)\z/;
But I would rather use the URI module:
my $last = (URI->new($url)->path_segments)[-1];

(In PHP) I would not use PCRE for such a trivial and un-ambiguous job. I would just do:
$parts = explode('/', rtrim($url, '/'));
$partYouWant = array_pop($parts);
EDIT
If you need to use PCRE (although I don't know why you would) this variation on eugene y's answer would do it:
$pattern = '#/([^/]+)\z#';
$url = 'http://example.com/sales/view/id/705';
preg_match($pattern, $url, $matches);
echo $matches[1];

In PHP you can do this in a single line code:
$url = 'http://example.com/sales/view/id/705';
substr($url, strrpos($url, '/') + 1);

Non PCRE alternative:
$url="http://example.com/sales/view/id/705";
$lastPart = current(array_reverse((explode('/',parse_url($url,PHP_URL_PATH)))));
Doubt if it's any faster though

You could use this pattern ([^\/]*)$ for everything from last / to end.
Maybe also interesting: ([^\/\?]*)(\?.*)?$ gives you everything between last / and first ?

Say no to PCRE if you can,:-).
echo basename('http://example.com/sales/view/id/705');

preg_match('#/([1-9]\d*)/?(?:$|\?)#', $url, $matches);//$matches[1] contains your id

Simplest:
$ok=preg_match('#\d+$#',$url,$m);
if($ok)
echo $m[0],"\n";
Brainy:
$ok=preg_match('#/(\d+)$#',$url,$m);
if($ok)
echo $m[1],"\n";
Flexible: (as it also allows words, other than digits)
$ok=preg_match('#/(\w+)$#',$url,$m);
if($ok)
echo $m[1],"\n";
More flexible: (as it now allows everything that's not a / to match)
$ok=preg_match('#/(.*?)$#',$url,$m);
if($ok)
echo $m[1],"\n";

Related

Using rtrim in php for specific purpose

I am not much used to using rtrim and Reg expressions. So I wanted to get my doubt cleared about this:
Here is a url: http://imgur.com/r/pics/paoWS
I am trying to use rtrim function on this url to pick out only the 'paoWs' from the whole url.
Here is what i tried:
$yurl = 'http://imgur.com/r/pics/paoWS';
$video_id = parse_url($yurl, PHP_URL_PATH);
$yid=rtrim( $video_id, '/' );
And i am using '$yid' to hotlink the image from imgur. But What I get after trying this function is:
$yid= '/r/pics/paoWS'
How do I solve this?
rtrim is used for trimming down a string of certain characters or whitespace on the right-hand side. It certainly shouldn't be used for your purpose.
Assuming the URL structure will always be the same, you could just do something like this:
$yurl = 'http://imgur.com/r/pics/paoWS';
$video_id = parse_url($yurl, PHP_URL_PATH);
$parts = explode('/', $video_id)
$yid = end($parts);
You sould not use regular expressions (whitch are 'expensive') for a so 'simple' problem.
If you want to catch the last part of the URL, after the last slash, you can do :
$urlParts = explode('/', 'http://imgur.com/r/pics/paoWS');
$lastPart = end($urlParts);
rtim( strrchr('http://imgur.com/r/pics/paoWS' , '/') ); rtrim + strrchr
substr(strrchr('http://imgur.com/r/pics/paoWS', "/"), 1); substr + strrchr
rtrim() returns the filtered value, not the stripped characters. And your usage of it isn't proper too - it strips the passed characters from the right side. And you don't need parse_url() either.
Proper answers have been given already, but here's a faster alternative:
$yid = substr($yurl, strrpos($yurl, '/')+1);
Edit: And another one:
$yid = ltrim(strrchr($yurl, '/'), '/');

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

PHP regex to search in the exact given pattern

i want to give regex a pattern and force it to read it all ..
http://example.com/w/2/1/x/some-12345_x.png
i want to target "some-12345_x"
i used this /\/(.*).png/, it doesnt work for some reason
how do i force it to remember it must start with / and end with .png?
If you always want to get the final file-name, minus the extension, you could use PHP's substr() instead of trying to come up with a regex:
$lastSlash = strrpos($url, '/') + 1;
$name = substr($url, $lastSlash, strrpos($url, '.') - $lastSlash);
Also, a more readable method would be to use PHP's basename():
$filename = basename($url);
$name = substr($filename, 0, strpos($filename, '.'));
To actually use a regex, you could use the following pattern:
.*/([^.]+).png$
To use this with PHP's preg_match():
preg_match('|.*/([^.]+).png$|', $url, $matches);
$name = $matches[1];
You can do:
^.*/(.*)\.png$
which captures what occurres after the last / till .png at the end.
You might need to use reg-ex in this situation for a particular reason, but here's an alternative where you don't:
$url = "http://example.com/w/2/1/x/some-12345_x.png";
$value = pathinfo($url);
echo $value['filename'];
output:
some-12345_x
pathinfo() from the manual
How about:
~([^/]+)\.png$~
this will match anything but / until .png at the end of the string.

Preg_replace domain problem

I'm Stuck try to get domain using preg_replace,
i have some list url
download.adwarebot.com/setup.exe
athena.vistapages.com/suspended.page/
prosearchs.com/se/tds/in.cgi?4&group=5&parameter=mail
freeserials.spb.ru/key/68703.htm
what i want is
adwarebot.com
vistapages.com
prosearchs.com
spb.ru
any body can help me with preg_replace ?
i'm using this http://gskinner.com/RegExr/ for testing :)
using preg_replace, if the number of TLDs is limited:
$urls = array( 'download.adwarebot.com/setup.exe',
'athena.vistapages.com/suspended.page/',
'prosearchs.com/se/tds/in.cgi?4&group=5&parameter=mail',
'freeserials.spb.ru/key/68703.htm' );
$domains = preg_replace('|([^.]*\.(?:com|ru))/', '$1', $urls);
matches everything that comes before .com or .ru which is not a period. (to not match subdomains)
You could however use PHPs builtin parse_url function to get the host (including subdomain) – use another regex, substr or array manipulation to get rid of it:
$host = parse_url('http://download.adwarebot.com/setup.exe', PHP_URL_HOST);
if(count($parts = explode('.', $host)) > 2)
$host = implode('.', array_slice($parts, -2));
Following code assumes that every entry is exactly at the beginning of the string:
preg_match_all('#^([\w]*\.)?([\w]*\.[\w]*)/#', $list, $m);
// var_dump($m[2]);
P.S. But the correct answer is still parse_url.
Why use a regular expression? Of course it is possible, but using this:
foreach($url in $url_list){
$url_parts = explode('/', $url);
$domains[] = preg_replace('~(^[^\.]+\.)~i','',$url_parts[0]);
}
$domains = array_unique($domains);
will do just fine;
maybe a more generic solution:
tested by grep, I don't have php environment, sorry:
kent$ echo "download.adwarebot.com/setup.exe
dquote> athena.vistapages.com/suspended.page/
dquote> prosearchs.com/se/tds/in.cgi?4&group=5&parameter=mail
dquote> freeserials.spb.ru/key/68703.htm"|grep -Po '(?<!/)([^\./]+\.[^\./]+)(?=/.+)'
output:
adwarebot.com
vistapages.com
prosearchs.com
spb.ru

PHP preg_match between text and the first occurrence of -

I'm trying to grab the 12345 out of the following URL using preg_match.
$url = "http://www.somesite.com/directory/12345-this-is-the-rest-of-the-url.html";
$beg = "http://www.somesite.com/directory/";
$close = "\-";
preg_match("($beg(.*)$close)", $url, $matches);
I have tried multiple combinations of . * ? \b
Does anyone know how to extract 12345 out of the URL with preg_match?
Two things, first off, you need preg_quote and you also need delimiters. Using your construction method:
$url = "http://www.somesite.com/directory/12345-this-is-the-rest-of-the-url.html";
$beg = preg_quote("http://www.somesite.com/directory/", '/');
$close = preg_quote("-", '/');
preg_match("/($beg(.*?)$close)/", $url, $matches);
But, I would write the query slightly differently:
preg_match('/directory\/(\d+)-/i', $url, $match);
It only matches the directory part, is far more readable, and ensures that you only get digits back (no strings)
This doesn't use preg_match but would achieve the same thing and would execute faster:
$url = "http://www.somesite.com/directory/12345-this-is-the-rest-of-the-url.html";
$url_segments = explode("/", $url);
$last_segment = array_pop($url_segments);
list($id) = explode("-", $last_segment);
echo $id; // Prints 12345
Too slow, I am ^^.
Well, if you are not stuck on preg_match, here is a fast and readable alternative:
$num = (int)substr($url, strlen($beg));
(looking at your code I guessed, that the number you are looking for is a numeric id is it is typical for urls looking like that and will not be "12abc" or anything else.)

Categories