PHP Get line matching phrase TXT File -


so i've got bunch of data in .txt in format

order_number first_name last_name email_address 

and i'm trying figure out how make if search example john_doe (has john_doe exact no john_do) grabs john_doe .txt , displays order_number first_name(john_doe in case) last_name email_address.

i have no clue how to, looked in preg_match didn't work out good. if me appreciated.

example requested:

"28dp9", "johan", "van dijk", "removed@gmail.com" 

you seem storing "comma-separated values" (csv). you'll need fetch data, sort , search it. take following data example:

"28dp9", "johan", "van dijk", "removed@gmail.com" "28dp9", "johan", "van dijk", "removed@gmail.com" "28dp9", "johan", "van dijk", "removed@gmail.com" "28dp9", "john_doe", "van dijk", "iam_john_doe@gmail.com" "28dp9", "johan", "van dijk", "removed@gmail.com" "28dp9", "johan", "van dijk", "removed@gmail.com" 

now there our 1 row want find (john_doe), need though sort data first associative array. that's done via combination of explode()/foreach():

$new = array(); $data_array = explode("\n", $data); foreach($data_array $element) {     list($order_number, $first, $last, $email) = explode(",", str_replace('"', '',$element));     $new[] = array(         'order_number' => $order_number,         'first_name' => $first,         'last_name' => $last,         'email' => $email,     ); } 

reference: $data data file (most fetched file_get_contents()). remove " data don't require it. that's done via str_replace()


now we've sorted it, need search it. done combination of array_map()/array_filter():

$search_word = 'john_doe';  $found = array_filter(array_map(function($element) use ($search_word){     if(trim($element['first_name']) == $search_word) {         return $element;      } }, $new)); 

the above search runs through each element (basically glorified foreach() loop.) , returns element has search word require. thus, search returns following example:

array (     [3] => array         (             [order_number] => 28dp9             [first_name] =>  john_doe             [last_name] =>  van dijk             [email] =>  iam_john_doe@gmail.com         )  ) 

example/demo