regex - Creating a PHP array from space delimited text file -


i have text file listing of directories turn array. figured space delimiting work number of spaces varies between each item , spaces in directory name problem. parse text php array.

the text file has rigid structure looks this:

04/17/2013  09:49 pm    <dir>          directory 1 (1994) 03/11/2013  06:48 pm    <dir>          director 2 (1951) 04/15/2013  08:34 pm    <dir>          going number 3 (2000) 08/17/2012  09:50 pm    <dir>          4 (1998) 10/17/2011  05:12 pm    <dir>          , lastly 5 (1986) 

i need keep folder date (not time), complete name of directory (as 1 entry) , year in parenthesis. in advance!

sure, use preg_split:

<?php $str = "04/17/2013  09:49 pm    <dir>          directory 1 (1994) 03/11/2013  06:48 pm    <dir>          director 2 (1951) 04/15/2013  08:34 pm    <dir>          going number 3 (2000) 08/17/2012  09:50 pm    <dir>          4 (1998) 10/17/2011  05:12 pm    <dir>          , lastly 5 (1986)";  function sp($x) {     return preg_split("/\s\s+|\s*\((\d{4}).*\)/", $x,0,preg_split_delim_capture); } $array = preg_split("/\n/", $str); $processed = array_map('sp', $array);  print_r($processed); 

this create array of arrays. each line become array, containing array each item. instance, $processed[0][3] contain this directory 1

keep in mind code assume spaces working division must 2 or more; 1 space considered part of same field. (you'll need hand hack according needs)

edit: added part year separated element of array. $processed[0][4] has 1994. (you don't need (), right?)

see working change here: http://codepad.org/in973ijv


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -