I have a csv upload in my site now when I try to do pregmatch for the first row of the csv, it is returning empty. See the code below.
$uploaded_file = $upload_dir . $_FILES['my_upload_file']['name'];
$uploaded_content = file( $uploaded_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
$regx_pattern = '/^(nickname|nick_name|family_name|\"nickname|"family_name)_/';
$first_row = array_shift( $uploaded_content );
$row_match = preg_match( $regx_pattern , $first_row, $matches );
var_dump($matches);
Result:
array
empty
But, if I declare $first_row as static value in my script by doing like this:
$first_row= 'nickname,nick_name_initial,nick_name_code';
It will return a match. Why isn’t it returning a match when I’m checking it from my csv data? Anyone encountered this?
If you want to ignore non alphanum and not quote characters at the begining of the string, you could do:
output: