ItalianOne
ItalianOne

Reputation: 241

Regex extract part of a link (php)

I got some link like:

/3/topic/video1148288/

and I want to take the number after video. I can't replace the link with only numbers because there are more before the actual video's id.

I tried

$embed = preg_match("#\b/video([0-9][0-9][0-9][0-9][0-9][0-9][0-9])/#", $raw);

But it doesn't work.

Any help?

Upvotes: 2

Views: 79

Answers (3)

paulie.jvenuez
paulie.jvenuez

Reputation: 295

preg_match('/(?<=video)\d+/i', $raw, $match);
echo $match[0];

Upvotes: 1

ederwander
ederwander

Reputation: 3478

$raw = "/3/topic/video1148288/";

preg_match("/video(\d+)/", $raw, $results);

print "$results[1]";

Upvotes: 1

jszobody
jszobody

Reputation: 28959

Give this a try:

$raw = "/3/topic/video1148288/";
preg_match("#/video(\d+)/#", $raw, $matches);
$embed = $matches[1];

Working example: http://3v4l.org/oLPMX

One thing to note from looking at your attempt, is that preg_match returns a truthy/falsely value, not the actual matches. Those are found in the third param ($matches in my example).

Upvotes: 1

Related Questions