I want to extract example and 1463512653 from this string with eregi i write basic Regular expression for this but I can’t continue this? Please help me to write Regular expression? http://www.flickr.com/photos/example/1463512653/ $string = "http://www.flickr.com/photos/example/1463512653/"; if (eregi("^(http://www.|http://|www.)(flickr.com/photos/)(I don’t know what write here)", $string, $phase)) echo "OK" ; echo "<pre>"; print_r($phase); PHP:
Well, I don't know how complex your work will be but here is dirty one: <?php $link = 'http://www.flickr.com/photos/example/1463512653/'; $splitfrom = preg_split('{(/)}i', $link, -1, PREG_SPLIT_NO_EMPTY); print_r($splitfrom); /* You will get Array ( [0] => http: [1] => www.flickr.com [2] => photos [3] => example [4] => 1463512653 ) Thanks to flickr the url will always be kinda the same so enjoy */ ?> PHP:
well... although I'm not a very experienced PHP programmer it seems a bit hard to extract both a word and a number from an URL - you can have one of the two - I think the method Mozzart suggested works quite perfectly. Other than that, I have no clue - sorry. Just my 2c. Good luck with this one.
Your first code example looks like you want to validate the URL as well, right? If that's the case, try this: if (preg_match('~^http://(?:www\.)?flickr\.com/photos/([\w-]+)/(\d+)/?$~', $string, $matches)) { // URL is valid, and here are the details you want: echo $matches[1]; // "example" echo $matches[2]; // "1463512653" } else { // Invalid URL } PHP: ALSO, don't use the ereg_* functions. The preg_* functions are about 6 times faster!
Eeep, I forgot about preg_match. You are better off using nico's solution since it'd be easier and more solid than mine. And yes, preg_* functions are faster (PCRE, hence the "p" following with "reg") anyway php.net strongly recommends you use PCRE instead of POSIX functions (POSIX functions aren't binary safe either) , although I forgot the "why" PCRE is better, well you can always google.