2

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?

1
  • Define doesn't work. It works and here's a demo. However I would replace all those numbers with [0-9]+ which means match a digit one or more times. If you are certain that there will always be 7 numbers, then you could use [0-9]{7}. Commented Nov 22, 2013 at 17:45

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

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

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

print "$results[1]";

2 Comments

What if the link was /3/topic/video1148288/foo? .* is not a very good idea, IMO.
1
preg_match('/(?<=video)\d+/i', $raw, $match);
echo $match[0];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.