php regEx

Discussion in 'PHP' started by dracula51, Dec 13, 2009.

  1. #1
    im a novice in php regEx...plz help me

    preg_match will return true if given link is like these:

    http://www.mydomain.com/path/file.php?dataid=[B]x92DFt[/B]_maradonna-hand_of_god-gallery&hr=1
    www.mydomain.com/path/file.php?dataid=[B]x92DFt[/B]_maradonna-hand_of_god-gallery&hr=1
    mydomain.com/path/file.php?dataid=[B]x92DFt[/B]_maradonna-hand_of_god-gallery&hr=1
    Code (markup):
    well i mean it will not care if http://www there or not
    2nd-> it will retrieve the id which is mixed with data name but they r separated by an underscore ( _ ) but plz notice, there cud be underscore inside the data name.
    another important thing is, id's length is unknown but it never contain sign but only A-Z a-z 0-9

    for example: x92DFt_maradonna-hand_of_god-gallery
    highlighted one is id & its separated by an underscore although there r underscore in the data name (maradonna-hand_of_god-gallery)

    help me plz
     
    dracula51, Dec 13, 2009 IP
  2. unigogo

    unigogo Peon

    Messages:
    286
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    unigogo, Dec 13, 2009 IP
    otakatun likes this.
  3. otakatun

    otakatun Well-Known Member

    Messages:
    388
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    108
    #3
    Instead of using regex, how about split it into array like below(the code not been tested though):

    And thanks unigogo for the link(that gonna help for the regex tester)

    As for the pattern, I believe it should be something like "/(\w+)_/" but forgive me if Im wrong
     
    otakatun, Dec 13, 2009 IP
  4. dracula51

    dracula51 Peon

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    how do u declare $ptn = "/x92DFt([\w_-]+)/"; id is variable...totally unknown
    if its known why shud i retrieve it :p

    & tnx for the link....not checked yet...will check it later
     
    dracula51, Dec 13, 2009 IP
  5. dracula51

    dracula51 Peon

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    trying & trying
    & failed :(

    ok. can anyone plz give me a preg_match() code of this link, so i'll understand how it works

    $url = 'http://www.dailymotion.com/video/xb3bcy_los-angeles-a-marijuana-smokers-hea_news';
    
    $pattern = ???? // i need this
    
    preg_match($pattern, $url, $match);
    
    echo $match[1]; // it will return only: xb3bcy
    PHP:
    yes...i want it only retrieve xb3bcy from the link
     
    dracula51, Dec 13, 2009 IP
  6. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    just use the regex coach, it's excellent for training you.

    assuming the string is always consistent you could use:

    $pattern = 'http://www.dailymotion.com/video/(.*?)_';
    PHP:
    or if you know that the string you're looking for will always be 6 chars long (inc. digits) then

    $pattern = 'http://www.dailymotion.com/video/(.{6})';
    PHP:
    and you don't even need the underscore (_) in that case!
     
    szalinski, Dec 13, 2009 IP
  7. dracula51

    dracula51 Peon

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    tnx szalinski :)
     
    dracula51, Dec 14, 2009 IP
  8. dracula51

    dracula51 Peon

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    just a quick question

    [a-z0-9] means what?? Alphabetic & numeric only?? or it includes special sign like @#$%&*-_=+
    if not what includes those sign & Alphabetic & numeric...i mean all
     
    dracula51, Dec 14, 2009 IP