在最后的正斜杠上分割URL

时间:2021-05-29 21:18:51

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:

我正在寻找一种方法,将Twitter permalink字符串分割为最后的斜线,这样我就可以存储tweet_id。两个例子:

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

Each URL has a similar format:

每个URL都有类似的格式:

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

With what regular expression would I easily capture every time?

每次我用什么正则表达式可以轻松捕获?

2 个解决方案

#1


21  

This is not a job for regular expressions, IMHO.

这不是正则表达式的工作,IMHO。

I would do this:

我想这样做:

$parts = explode('/', rtrim($url, '/'));
$id_str = array_pop($parts);

#2


2  

REGX ? yes; simple one liner.

REGX吗?是的,简单的一个班轮。

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

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


Perl

Perl

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

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

print $f1;

打印$ f1;


PHP

PHP

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

$ f1 = preg_replace(“* \ / / / ^。”,“”,$ url);

echo $f1;

echo $ f1;

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

编辑:反斜杠没有展示;固定在编辑框用双\ (wierd!)

#1


21  

This is not a job for regular expressions, IMHO.

这不是正则表达式的工作,IMHO。

I would do this:

我想这样做:

$parts = explode('/', rtrim($url, '/'));
$id_str = array_pop($parts);

#2


2  

REGX ? yes; simple one liner.

REGX吗?是的,简单的一个班轮。

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

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


Perl

Perl

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

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

print $f1;

打印$ f1;


PHP

PHP

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

$ f1 = preg_replace(“* \ / / / ^。”,“”,$ url);

echo $f1;

echo $ f1;

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

编辑:反斜杠没有展示;固定在编辑框用双\ (wierd!)