Help regarding preg_match....

Discussion in 'PHP' started by app_1, Oct 14, 2007.

  1. #1
    Hi,

    Can anyone help in writing preg_match for the line
    2007-10-08 00:00:05 W3SVC31713 216-55-183-237 216.55.183.237 GET /jwalker.swf - 80 - 98.194.15.17

    Plz help me regrading this..


    Cheers,
    app_1:)
     
    app_1, Oct 14, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    What do you want to do with this string? Which part do you want to get?
     
    nico_swd, Oct 15, 2007 IP
  3. app_1

    app_1 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I want to all the values of that line.
    like.. arr[0] = 2007-10-10
    arr[1] = 00:00:11 and so on....
     
    app_1, Oct 15, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    I think you're better off using sscanf() here.

    
    $string = '2007-10-08 00:00:05 W3SVC31713 216-55-183-237 216.55.183.237 GET /jwalker.swf - 80 - 98.194.15.17';
    
    $data = sscanf($string, '%s %s %s %s %s GET %s - %d - %s');
    
    print_r($data);
    
    PHP:

    Edit:

    Or maybe even exploding the string on the spaces using explode()
     
    nico_swd, Oct 15, 2007 IP
  5. app_1

    app_1 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    HTTP/1.1 Mozilla/5.0+(Windows;+U;+Windows+NT+5.1;+en-US;+rv:1.8.1.7)+Gecko/20070914+Firefox/2.0.0.7
    how to parse this ???

    the line is
    2007-10-08 00:00:05 W3SVC31713 216-55-183-237 216.55.183.237 GET /jwalker.swf - 80 - 98.194.15.17 HTTP/1.1 Mozilla/5.0+(Windows;+U;+Windows+NT+5.1;+en-US;+rv:1.8.1.7)+Gecko/20070914+Firefox/2.0.0.7
     
    app_1, Oct 15, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    
    $data = sscanf($string, '%s %s %s %s %s GET %s - %d - %s %s %s');
    
    PHP:
     
    nico_swd, Oct 15, 2007 IP
  7. app_1

    app_1 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    hey,i got that one..thanks..
    but next field i have like www.magicaltransformation.com,but its taking as
    arr[2] = www.magictr
    rest of charater not getting.
    Any idea ???
     
    app_1, Oct 15, 2007 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    Could you maybe post the whole string for once? It's hard to guess how it looks like.
     
    nico_swd, Oct 15, 2007 IP
  9. app_1

    app_1 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    ok.. line is
    2007-10-08 00:00:05 W3SVC31713 216-55-183-237 216.55.183.237 GET /jwalker.swf - 80 - 98.194.15.17 HTTP/1.1 Mozilla/5.0+(Windows;+U;+Windows+NT+5.1;+en-US;+rv:1.8.1.7)+Gecko/20070914+Firefox/2.0.0.7 - http://www.ravenwilliams.com/index.php?option=com_content&task=view&id=116&Itemid=105 www.magicaltransformations.com



    and output i am getting is

    parser started at 1192442071.88Array ( [0] => 2007-10-08 [1] => 00:00:05 [2] => W3SVC31713 [3] => 216-55-183-237 [4] => 216.55.183.237 [5] => GET [6] => /jwalker.swf [7] => 80 [8] => 98.194.15.17 [9] => HTTP/1.1 [10] => Mozilla/5.0+(Windows;+U;+Windows+NT+5.1;+en-US;+rv:1.8.1.7)+Gecko/20070914+Firefox/2.0.0.7 [11] => http://www.ravenwilliams.com/index.php?option=com_content&task=view&id=116&Itemid=105 [12] => www.magicaltr )
    not getting full data...
    y ??
     
    app_1, Oct 15, 2007 IP
  10. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #10
    
    $data = sscanf($string, '%s %s %s %s %s GET %s - %d - %s %s %s - %s %s');
    
    PHP:
     
    nico_swd, Oct 15, 2007 IP
  11. app_1

    app_1 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    i used ssanf,but that last data www.magaicaltranformation.com not getting fully
    getting only www.magtran
    is there any max limit to array ????
     
    app_1, Oct 15, 2007 IP
  12. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #12
    I tested it and it's working for me. Did you try the last line of code I gave you?

    I get this output.
    
    Array
    (
        [0] => 2007-10-08
        [1] => 00:00:05
        [2] => W3SVC31713
        [3] => 216-55-183-237
        [4] => 216.55.183.237
        [5] => /jwalker.swf
        [6] => 80
        [7] => 98.194.15.17
        [8] => HTTP/1.1
        [9] => Mozilla/5.0+(Windows;+U;+Windows+NT+5.1;+en-US;+rv:1.8.1.7)+Gecko/20070914+Firefox/2.0.0.7
        [10] => http://www.ravenwilliams.com/index.php?option=com_content&task=view&id=116&Itemid=105
        [11] => www.magicaltransformations.com
    )
    
    Code (markup):
     
    nico_swd, Oct 15, 2007 IP
    tarponkeith likes this.