eregi Regex help

Discussion in 'PHP' started by phpmania, Jul 21, 2008.

  1. #1
    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:

     
    phpmania, Jul 21, 2008 IP
  2. Mozzart

    Mozzart Peon

    Messages:
    189
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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:
     
    Mozzart, Jul 21, 2008 IP
  3. CristianR

    CristianR Peon

    Messages:
    704
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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.
     
    CristianR, Jul 21, 2008 IP
  4. phpmania

    phpmania Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thank you so much Mozzart and CristianR.
     
    phpmania, Jul 22, 2008 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    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!
     
    nico_swd, Jul 22, 2008 IP
  6. Mozzart

    Mozzart Peon

    Messages:
    189
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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.
     
    Mozzart, Jul 22, 2008 IP