简体   繁体   中英

Regex with PHP - Can't add preg_grep indexes to file

So, I need to make a script that reads a .csv file, find name: xxxxx with regex, and add the regex results to a txt file.

My Php Script:

<?PHP
$array=array();
$file_handle = fopen("CONTACTOS.CSV", "r");
$fp=fopen("teste.txt", "w");
$pattern = '/Nome.+\s+/';
while (!feof($file_handle) ) {
    $i=0;
    $line_of_text = fgetcsv($file_handle, 1024);
    $iend=count($line_of_text);
            while($i<=$iend){
                    if (isset($line_of_text[$i])){
                            //$array[]=$line_of_text[$i];
                            array_push($array, $line_of_text[$i]);
                            //fputs($fp, $line_of_text[$i]);
                            //echo($array[$i] . "\n");
                            $i++;
                    }
                    else{
                            $i++;
                        }
            }
}
//print_r($array);
$fl_array = preg_grep("/Nome.+\s+/", $array);
print_r($fl_array);
// ________________________________________
$e=1;
end($fl_array);
$flend=key($fl_array);
reset($fl_array);
$arrayF = array_values($fl_array);
while($e<=$flend){
    if (array_key_exists($e, $arrayF) == true) { 
            //print_r($e . "\n");
            puts($fp, $arrayF[$e]);
            $e++;
        }
    else{
        $e++;
        }
    }
// ________________________________________
//print_r($fl_array);
$fp = fopen('CONTACTOS_NOVO.csv', 'w');
       //fputcsv($fp, $array);
fclose($fp);
fclose($file_handle);

?>

Part of CSV File:

De: bpsnun@m2g-24-59.spvservers.net [mailto:bpsnun@m2g-24-59.spvservers.net] Em nome de Site - BPS Finance
Enviada: quinta-feira, 18 de Março de 2010 10:57
Para: form@bpsfinance.com
Assunto: Formulário - BPS Finance



Formulário nº: 002136

Data: 18/03/2010 - 11:47:58

Faça JÁ o seu Pedido



Montante Pretendido   



Montante de Crédito Pretendido*:   4000 €

1.  O Preenchimento desta proposta não obriga o cliente à sua aceitação.
2.  Caso algum dos proponentes e/ou seus cônjuges tiverem Incidentes Bancários, a proposta será imediatamente RECUSADA.
3.  Não existem quaisquer custos associados à emissão da Pré-Aprovação.
4.  PRAZO: De 12 a 96 meses. Apresentaremos todas as prestações possíveis para o seu crédito. E você Escolhe!

1º Titular   



Nome*:   MARIA JOAO MOTA

I want to save in my $fp .txt file only the Nome*: XXXX XXXXXXX XXXX

What is the problems with my code? The error I'm getting with this code is:

Fatal Error: Call to undefined function puts() in C:\XXX\teste.php on line 35

Can you help me remade the code to make it work?

UPDATE! Ok no errors now, but the preg_grep isnt wornking, my txt have the same content as the .csv file, and not only the nome*: How can i make my preg_grep work?

In

while($e<=$flend){
    if (array_key_exists($e, $arrayF) == true) { 

you are checking array keys starting from $e=1; but the found Nome line is stored in $arrayF[0] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM