Simplemente especifique un nombre de archivo, obtenga el contenido del archivo y haga corresponder la expresión regular con el contenido del archivo. Ver this y this para obtener más detalles con respecto a mi ejemplo de código a continuación:
$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: Esto funciona independientemente de si está o no está en una máquina Linux.
¡Gracias! Eso funcionó de maravilla, pero ¿cómo puedo obtener un "código" específico en una línea? Una línea se ve así: 'TME: ... | UID: ... | FNE: ... | MSG: ... | IPA: ...'. El "código" es, por ejemplo, 'UID'. – Erik
Si te entiendo correctamente, 'if (strpos ($ line, '| UID:')! == false)' podría hacer lo que necesites. – ghbarratt
¿Cómo debe estar el eco dentro de este si? – Erik