Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split URL on final forward slash

Tags:

regex

php

I'm looking for a way to split a Twitter permalink string on its final forward slash, so I can store the tweet_id. Two examples:

http://twitter.com/bloombergnews/status/55231572781170688
http://twitter.com/cnn/status/55231572781170688

Each URL has a similar format:

http://twitter.com/<screen_name>/status/<id_str>

With what regular expression would I easily capture every time?

like image 618
Pr0no Avatar asked Jan 03 '12 17:01

Pr0no


2 Answers

This is not a job for regular expressions, IMHO.

I would do this:

$parts = explode('/', rtrim($url, '/'));
$id_str = array_pop($parts);
like image 186
DaveRandom Avatar answered Oct 30 '22 05:10

DaveRandom


REGX ? yes; simple one liner.

$url = 'http://twitter.com/bloombergnews/status/55231572781170688';


Perl

($f1 = $url) =~ s/^.*\///;

print $f1;


PHP

$f1 = preg_replace("/^.*\//","",$url);

echo $f1;

EDIT: backslashes didn't show; fixed in edit box by double \\ (wierd!)

like image 25
Wombat_RW Avatar answered Oct 30 '22 03:10

Wombat_RW