You can do it in a very simple way:
$start = 'Email:';
$end = ',';
$pos1 = strpos( $log, $start );
$pos2 = strpos( $log, $end, $pos1 );
$block = substr(
$log, $pos1 + strlen( $start ),
$pos2 - $pos1 - strlen( $start )
);
See working on IDEONE.
Of course, for the specific case it is possible to write in a much more summarized way:
$pos1 = strpos( $log, 'Email:' );
$pos2 = strpos( $log, ',', $pos1 );
$block = substr( $log, $pos1 + 6, $pos2 - $pos1 - 6 );
If you need to test cases where there is no field Email:
in the log:
$pos1 = strpos( $log, $start );
if( $pos1 === false ) die( 'Campo não encontrado' ); // ou return ''; se usar em função.
...
Creating a function:
In general, you can have a function to extract the data you want. There are a thousand ways to do it, this is one of them:
function my_extract( $text, $start, $end ) {
$pos1 = strpos( $text, $start );
if( false === $pos1 ) return 'Não encontrado';
$pos1 += strlen( $start );
$pos2 = strpos( $text, $end, $pos1 );
return trim( substr( $text, $pos1, $pos2 - $pos1 ) );
}
Mode of use:
$nome = my_extract( $log, 'Nome:' , ',' );
$email = my_extract( $log, 'Email:', ',' );
See demonstration on IDEONE.
Using cannon to kill dove:
Since fatally someone would end up posting, follows a solution with Regex:
if( preg_match( '/Email:\s*(.*)\s*,/', $log, $matches ) ) {
$email = $matches[1];
} else {
$email = ''; // Não encontrado
}
// podia ser um operador ternário, mas não é o foco da pergunta,
// não ajuda na leitura do código e não ajuda na performance.
Again, see working on IDEONE.
If only to find an occurrence of string, do not recommend. It seems simple, but internally the function does a lot more than you need for the proposed problem.
Description of the regular expression:
/ / delimitadores
Email: string procurada
\s* \s* espaços em branco
(.*) grupo que queremos retornar (quaisquer caracteres)
, marcador do final
Extra considerations:
If you want to use accentuated strings in the future, such as "Profession:", and the encoding of your text is multibyte (as UTF-8) for example, instead of strpos
use mb_strpos
and configure your PHP for encoding correct.
As mentioned by fellow @lvcs, in case you have any situation where you want to locate both Email
as EMAIL
or eMaIl
, can change the strpos
for stripos
, or the mb_strpos
for mb_stripos
In case you want to insensitive search with Regex, you have to add the flag i
at the end of the expression (add a i
after the last bar).
If you really want to ensure that the string Email:
not be confused with something in the middle of the line, can specify "$Email:"
as a marker to include the line break in the query, and the flag m
for multiline research.
Always has
Email:
before and the comma after?– Sergio
Yes, that’s the structure
– Thiago
And it also has more fields before and after, but I only put these 2 (name,email) to show the structure of the file.
– Thiago
is an email field in the whole file, or are several?
– Bacco
Only a field written "Email', the others are other information, only to confirm that the file is larger.
– Thiago