Thursday, 15 March 2012

parsing - Parse a text file in PHP, where the format of data changes -



parsing - Parse a text file in PHP, where the format of data changes -

my info looks this.

123456 abc1 (aaa: [bbb]aaa) http://exampleurl.org.uk 654321 cba2 (bbb: aa7) http://urlexample.org.uk ...

i split each row 3 strings, can set them array. e.g:

string 1 = 123456 string 2 = abc1 (aaa: [bbb]aaa) string 3 = http://exampleurl.org.uk

my problem sec string changing lots of different formats. first string similar, , lastly string url.

edit: have noticed of 2nd strings end bracket ')' if helps.

i going utilize explode() i'm not sure how deal sec string. advice?

although utilize regex, option:

function parse($str) { $parts = explode(" ", $str); // first part of exploded str $number = array_shift($parts); // lastly part of exploded str $url = array_pop($parts); // thats left middle str $between = trim(implode(" ", $parts), " "); homecoming array($number, $between, $url); } $str = "123456 abc1 (aaa: [bbb]aaa) http://exampleurl.org.uk"; print_r(parse($str));

output:

array (size=3) 0 => string '123456' (length=6) 1 => string 'abc1 (aaa: [bbb]aaa)' (length=20) 2 => string 'http://exampleurl.org.uk' (length=24)

php parsing text-files

No comments:

Post a Comment