php - Stepping through a file after pattern match -


i trying acquire first , last name fields file between blocks of text. have tried php, awk, , perl can't want.

the data:

[abc-001] first=firstabc001 phone=sometext last=sometext  [def-001] phone=sometext last=sometext first=firstdef001  [abc-002] phone=sometext first=firstabc002 last=sometext  [ghi-001] first=firstghi001 phone=sometext last=sometext 

function call: get_firstname( $w=abc, $n=002 )

search , return: firstabc002


i trying create php site grabs first name values send values stored in flat file.

i don't care language long can call php ( shouldn't problem ).

i have tried php, awk , perl couldn't work.


i need return value. not sure if flat file should read in 1 big string, array, or loop through , each line , step through after reached [abc-002] header, etc.

i have tried online regex utilities php preg_match , cant array[n] show fisrtabc002 results


thanks submitted answer help. didn't know parse_ini_(file|string) http://php.net/manual/en/function.parse-ini-file.php

i mark best answer in little bit when further testing.

i perl , awk options have had need functions @ different times , in bag of tricks.

using parse_ini can code like:

function get_firstname( $w, $n ){     $ini_array = parse_ini_file("test.ini", true);     if($ini_array["$w-$n"] , $ini_array["$w-$n"]["first"]){         return "first name:" . $ini_array["$w-$n"]["first"];     }else{         return "not found";     } }  echo get_firstname("abc","002"); //first name:firstabc002 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -