Basta specificare un nome file, ottenere il contenuto del file ed eseguire la regex corrispondente al contenuto del file. Vedere this e this per ulteriori dettagli per quanto riguarda il mio codice di esempio qui sotto:
$fileName = '/path/to/file.txt';
$fileContents = file_get_contents($fileName);
$searchStr = 'I want to find this exact string in the file contents';
if ($fileContents) { // file was retrieved successfully
// do the regex matching
$matchCount = preg_match_all($searchStr, $fileContents, $matches);
if ($matchCount) { // there were matches
// $match[0] will contain the entire string that was matched
// $matches[1..n] will contain the match substrings
}
} else { // file retrieval had problems
}
Nota: Questo funziona indipendentemente dal fatto che non sei su una macchina Linux.
Grazie! Ha funzionato come un incantesimo, ma come posso ottenere un "codice" specifico in una riga? Una riga si presenta così: 'TME: ... | UID: ... | FNE: ... | MSG: ... | IPA: ...'. Il "codice" è per esempio 'UID'. – Erik
Se ho capito bene, 'if (strpos ($ line, '| UID:')! == false)' potrebbe fare quello che ti serve. – ghbarratt
Come dovrebbe essere l'eco all'interno di questo se? – Erik