I'm trying to read through a csv file and extract the data thanks to various regex. I do not have access to the imported csv file content.
However, it is possible that one or more lines are empty. For that, it's possible to use trim function(). The problem is to know how to adapt my various arrays to recover the empty lines
On this example, two lines are empty for the phonenumber, so how can I detect that and how to insert these empty lines in my phones array ?
For example, if I do :
foreach($fullNames as $fullName)
{
echo $fullName."<br>";
}
foreach($phones as $phone)
{
echo $phone."<br>";
}
The result will be :
{Marc Vador, Marc Vador, Marc Vador, Marc Vador, Marc Vador}
{0692 10 10 10, 0692 10 10 10,0692 10 10 10}
what I want to achieve is this :
{Marc Vador, Marc Vador, Marc Vador, Marc Vador, Marc Vador}
{0692 10 10 10, , 0692 10 10 10, , 0692 10 10 10}
$emptyValue = "";
if (($handle = fopen($loadedSheetName.'.csv', "r")) !== FALSE)
{
fgetcsv($handle);
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE)
{
$col = count($data);
for($c = 0; $c < $col; $c++)
{
$phones = array();
$mails = array();
$zipcodes = array();
$fullNames = array();
if ('' === trim($data[$c]))
{
$emptyValue = "";
}
if(preg_match('/^(0)(692|693|262)(\d{6})$/', $data[$c], $matches))
{
$phones[] = "+262".$matches[2].$matches[3];
}
if(preg_match('/^(0)(692|693|262)( )(\d{2})( )(\d{2})( )(\d{2})$/', $data[$c], $matches))
{
$phones[] = "+262".$matches[2].$matches[4].$matches[6].$matches[8];
}
if(preg_match('/^(0)(692|693|262)( )(\d{2})( )(\d{2})( )(\d{2})(\/)(0)(692|693|262)( )(\d{2})( )(\d{2})( )(\d{2})$/', $data[$c], $matches))
{
$phones[] = "+262".$matches[2].$matches[4].$matches[6].$matches[8].$matches[9]."+262".$matches[11].$matches[13].$matches[15].$matches[17];
}
if(preg_match('/^([^\W][a-zA-Z0-9_]+)(\.[a-zA-Z0-9_]+)*(\@)([a-zA-Z0-9_]+)*(\.[a-zA-Z]{2,4})$/', $data[$c], $matches))
{
$mails[] = $matches[0];
}
if(preg_match('/^(Sainte|Saint|saint|sainte)(-)([a-zA-z]+)$/', $data[$c], $matches))
{
$zipcodes[] = $matches[0];
}
if(preg_match('/^(([a-zA-Z\W]+)( )([a-zA-Z\W]+))$/', $data[$c], $matches))
{
$fullNames[] = $matches[0];
}
if(preg_match('/^(([a-zA-Z\W]+)( )([a-zA-Z\W]+)( )([a-zA-Z\W]+))$/', $data[$c], $matches))
{
$fullNames[] = $matches[0];
}
}
}
fclose($handle);
}