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.
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:
The second options works fine. I try to understand what is the logic of $key < 4 ... trying to do? Could you explain. Thanks alot.
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.
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 ?
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.
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.