Save information in a csv without being repeated (php)

0

I am looking for a way to save variables in a csv file. Here is an example of what has been achieved:

$enlace="http://php.net/manual/es/function.fgetcsv.php";

$Z = md5($enlace);
$A = substr($Z,0,2); //Obtenemos los primeros 2 caracteres del hash
$B = substr($Z,16,2); //Obtenemos 2 caracteres a partir del caracter 16
$C = substr($Z,30,2); //Obtenemos 2 caracteres a partir del caracter 30
$D = substr($Z,23,1); //Obtenemos 1 caractere a partir del caracter 23
$name = $A.$B.$C.$D; //Juntamos todo    

$f = fopen("demosaved.csv", "a");
fputcsv($f, array($name, $enlace));
fclose($f);

I explain the variable $ link is not the same when I update the page but sometimes it repeats and ends up saving although it is repeated.

What I'm looking for is some way to compare the contents of the csv and if the variable $ name and $ link is found inside the csv file, do not save the columns Thanks

    
asked by BotXtrem Solutions 03.10.2017 в 06:06
source

1 answer

0

It took me time but I found the simplest solution:

$enlace="http://php.net/manual/es/function.fgetcsv.php";

$Z = md5($enlace);
$A = substr($Z,0,2); //Obtenemos los primeros 2 caracteres del hash
$B = substr($Z,16,2); //Obtenemos 2 caracteres a partir del caracter 16
$C = substr($Z,30,2); //Obtenemos 2 caracteres a partir del caracter 30
$D = substr($Z,23,1); //Obtenemos 1 caractere a partir del caracter 23
$name = $A.$B.$C.$D; //Juntamos todo    

$RN = $name.",".$enlace;

$pagina = file_get_contents('demosaved.csv');
$pos = strpos($pagina, $RN);

// Nótese el uso de ===. Puesto que == simple no funcionará como se espera
if ($pos === false) {
    echo "La cadena '$RN' no fue encontrada en la cadena dada <br/>";
$f = fopen("demosaved.csv", "a");
fputcsv($f, array($name, $enlace));
fclose($f);
} else {
    echo "La cadena '$RN' fue encontrada en la cadena dada";
    echo " y existe en la posición $pos";
}

Well I hope it will be useful to anyone else. Saludes

    
answered by 04.10.2017 / 04:56
source