Need Help PHP Regexp

Discussion in 'PHP' started by khairulanuarmm, Dec 5, 2007.

  1. #1
    Hi everyone,

    I am going to extract some content from text file crosssing multi-line of records before posting them to mysql db.
    The following conditions need be considered:-

    Text File Content:

    Computer Properties:
    Operating System Win XP Prof
    OS Services Pack Services Pack 2
    Client Name Comp123

    Storage:
    Disk #1 MAX 41GB
    Disk #2 ST42120
    SPD Properties:
    Memory Slots 4

    I need to extract by using php regular expression into an array like this:-
    $arr['Computer Properties']['Operating System']="Win XP Prof"
    $arr['Computer Properties']['OS Serives Pack']="Services Pack 2"
    $arr['Computer Properties']['Client Name']="Comp123"
    $arr['Storage']['Disk #1']="MAX 41GB"
    etc

    Look for helps.thanks in advance.
     
    khairulanuarmm, Dec 5, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Perhaps you should consider storing the details in .ini format, and then you can just use parse_ini_file(). It's much cleaner and faster than a regex.

    
    [Computer Properties]
    Operating System = Win XP Prof
    OS Services Pack = Services Pack 2
    Client Name Comp123 =
    
    [Storage]
    Disk #1 = MAX 41GB
    Disk #2 = ST42120
    SPD Properties =
    Memory Slots 4 =
    
    Code (markup):
    
    
    $arr = parse_ini_file('your-file.ini', true);
    
    print_r($arr);
    
    PHP:
    Outputs:
    
    Array
    (
        [Computer Properties] => Array
            (
                [Operating System] => Win XP Prof
                [OS Services Pack] => Services Pack 2
                [Client Name] => Comp123
            )
    
        [Storage] => Array
            (
                [Disk #1] => MAX 41GB
                [Disk #2] => ST42120
                [SPD Properties] => 
                [Memory Slots 4] => 
            )
    
    )
    
    
    
    Code (markup):
    EDIT:

    After rereading your post, that might not be an option. So try this:
    
    $data = file('details.txt');
    $arr = array();
    
    foreach ($data AS $key => $line)
    {
    	if (preg_match('~^(Operating System|OS Services Pack|Client Name|Disk #\d+|SPD Properties|Memory Slots)[\s:]+([^$]*)$~i', trim($line), $match))
    	{
    		$arr[($key < 4 ? 'Computer Properties' : 'Storage')][$match[1]] = $match[2];
    	}
    }
    
    print_r($arr);
    
    
    PHP:
     
    nico_swd, Dec 5, 2007 IP
  3. khairulanuarmm

    khairulanuarmm Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The second options works fine. I try to understand what is the logic of $key < 4 ... trying to do? Could you explain.
    Thanks alot.
     
    khairulanuarmm, Dec 5, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    It'll manage the array keys.

    For the first three properties (OS, SP, Client) it'll use "Computer Properties", and for the rest "Storage". It's just to get the array like you wanted in your example above.
     
    nico_swd, Dec 5, 2007 IP
  5. khairulanuarmm

    khairulanuarmm Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If I add some other properties before storage: such as
    computer properties:
    Operating System Win XP Prof
    OS Services Pack Services Pack 2
    Client Name Comp123

    motherboard:
    model id PM400-xxx-yyy
    BIOS date 12/12/2006
    ..
    storage:
    disk #1 bla-bla

    How to make it dynamic bcoz in actual input file the key line position may change ?
     
    khairulanuarmm, Dec 5, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    This would end up with complicated regular expressions. Store the contents in a valid ini format. It's so much easier to maintain and parse.
     
    nico_swd, Dec 5, 2007 IP
  7. khairulanuarmm

    khairulanuarmm Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I try to use your first option since i want it dynamic position. Reformat the input text file to the format you suggested. But I found parse_ini_file threw an error when bracket open or close bracket exist inside the text such as
    Operating System= Win XP Prof (Win Retail) //error occure here
    OS Services Pack= Services Pack 2
    Client Name= (Comp123) //it works here

    I am going replace all symbols ( to { and ) to }. How do i create code using preg_replace or regxp ?
    Note : I have substituted this symbol without using reg exp.
     
    khairulanuarmm, Dec 5, 2007 IP