How to search in an array with preg_grep using php

The PHP function, preg_grep, is used to search an array for specific patterns and then return a new array based on that filtering follow below code with string to array and array to search specifc word(character). You may also like PHP array_search() Function and How to search record in mysql codeigniter.

Code

$Item = $_POST['item'];
$getitem =  nl2br($Item);
$haystack =  explode('<br />',$getitem);
$trimmed_array=array_map('trim',$haystack);

$EMS        = preg_grep ('/^E(\w+)/i', $trimmed_array);
$Parcel     = preg_grep ('/^C(\w+)/i', $trimmed_array);
$Registered = preg_grep ('/^R(\w+)/i', $trimmed_array);
$Traffic    = preg_grep ('/^T(\w+)/i', $trimmed_array);
$court      = preg_grep ('/^M(\w+)/i', $trimmed_array);

Leave a Reply

Your email address will not be published. Required fields are marked *