2013-03-07 12 views
6

E 'possibile verificare se la stringa che viene aggiunta al file non è già presente nel file e solo dopo aggiungerla? In questo momento sto usandoPhp accoda al file se la stringa è univoca

 $myFile = "myFile.txt"; 
    $fh = fopen($myFile, 'a') or die("can't open file"); 
    $stringData = $var . "\n"; 
    fwrite($fh, $stringData); 
    fclose($fh); 

Ma ho molti duplicati di $ var valori e voleva sbarazzarsi di loro. Grazie

+0

Puoi chiarire di più il $ var – Ranjit

+0

Ulteriori informazioni renderebbero più semplice rispondere a questa domanda. Il file è sempre delimitato da delimitazione? La lunghezza della stringa è fissa? Il file è piccolo (* fattibile per 'file_get_contents' *) o gargantuan? – Dan

risposta

3

uso questo

$file = file_get_contents("myFile.txt"); 
if(strpos($file, $var) === false) { 
    echo "String not found!"; 
    $myFile = "myFile.txt"; 
    $fh = fopen($myFile, 'a') or die("can't open file"); 
    $stringData = $var . "\n"; 
    fwrite($fh, $stringData); 
    fclose($fh); 
} 
+1

+1 quasi la stessa ora :) – Rikesh

+1

cosa succede se la stringa trovata nella posizione 0 –

+0

@Akam, può essere risolta con 'strpos() === FALSE' – fedorqui

0

possibile soluzione potrebbe essere:

1. Fetch the contents using fread or file_get_contents 
2. Compare the contents with the current contents in file 
3. add it if it is not there. 
0
function find_value($input) { 

    $handle = @fopen("list.txt", "r"); 
    if ($handle) { 
    while (!feof($handle)) { 
    $entry_array = explode(":",fgets($handle)); 
    if ($entry_array[0] == $input) { 
     return $entry_array[1]; 
    } 
    } 
fclose($handle); 
} 
return NULL; 
} 

Si può fare lo in questo modo anche

$content = file_get_contents("titel.txt"); 
$newvalue = "word-searching"; 
//Then use strpos to find the text exist or not 
1

Il modo migliore è l'uso file_get_contents & esegue l'operazione solo se $ var non è nel tuo file.

$myFile = "myFile.txt"; 
$file = file_get_contents($myFile); 
if(strpos($file, $var) === FALSE) 
{ 
    $fh = fopen($myFile, 'a') or die("can't open file"); 
    $stringData = $var . "\n"; 
    fwrite($fh, $stringData); 
    fclose($fh); 
} 
+1

'if (! Strpos (" some text "," some ")) { echo" non trovato "; } 'controlla questo! –

1
$myFile = "myFile.txt"; 
$filecontent = file_get_contents($myFile); 
if(strpos($filecontent, $var) === false){ 
$fh = fopen($myFile, 'a') or die("can't open file"); 
$stringData = $var . "\n"; 
fwrite($fh, $stringData); 
fclose($fh); 
}else{ 
//string found 
} 
0

si potrebbe salvare tutti la stringa aggiunto in un array e il controllo con in_array se la stringa corrente è stato aggiunto o meno.

La seconda scelta è read the file ogni volta che si desidera scrivere e fare un strstr su di esso.

0

Credo che fgets è la risposta qui.

$handle = fopen($path, 'r+');  // open the file for r/w 
while (!feof($handle)) {   // while not end 
    $value = trim(fgets($handle)); // get the trimmed line 
    if ($value == $input) {  // is it the value? 
     return;     // if so, bail out 
    }        // 
}         // otherwise continue 
fwrite($handle, $input);   // hasn't bailed, good to write 
fclose($handle);     // close the file 

Questa risposta si basa unicamente sul fatto che si è aggiunto un ritorno a capo ("\n") nel codice, che è il motivo per cui fgets funzionerà qui. Questo può essere preferibile rispetto a trascinare l'intero file in memoria con file_get_contents(), semplicemente perché la dimensione del file potrebbe essere proibitiva.

In alternativa, se i valori non sono Newline delimitati, ma sono a lunghezza fissa, è sempre possibile utilizzare la $length argomento fgets() tirare esattamente $n caratteri su (o usare fread() per tirare esattamente $n byte su)

Problemi correlati