php - Extract a Flickr photo ID from URL

932

I need to extract a piece of a URL in PHP. From the URLhttp://www.flickr.com/photos/[email protected]/7885410582 I need only the7885410582 part.

My code:

$string2 = 'http://www.flickr.com/photos/[email protected]/7885410582';
preg_match_all('~s/ (.*?) /~i',$string2,$matches, PREG_PATTERN_ORDER);
echo $matches[1][0] . '<br />';

Can anyone look at it and correct it for me? Thanks in advance.

318

Answer

Solution:

you can use

$data = explode("/",$url)
$value = $data[sizeof($data)-1]
964

Answer

Solution:

try this regex:

\d+$

will find all digits before string end

or:

(?<=/)\d+$

will find all digits after/ sign before string end

998

Answer

Solution:

If the identifier you want is always the last in the URL, don't bother with regular expressions and instead use simple string functions.

<?php

$url = 'http://www.flickr.com/photos/[email protected]/7885410582';

$flickr_id = substr($url, strrpos($url, "/") + 1);

echo $flickr_id; // 7885410582
820

Answer

Solution:

Try this:

preg_match_all('~^.+/(\d+)$~',$string2,$matches);

Description

Regular expression visualization

Demo

http://regex101.com/r/aL2hL3

Sample code

$string2 = 'http://www.flickr.com/photos/[email protected]/7885410582';
preg_match('~^.+/(\d+)$~',$string2,$matches);
echo  $matches[1]; // 7885410582

People are also looking for solutions to the problem: .htaccess - How to handle GET-Parameter in PHP when modrewrite is activated?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.