Get a specific part of a string

4

I need to get a specific part of a log file, its structure looks something like this:

##################################################
----------------------------------------
Nome: nome_user,
Email: [email protected],
-------------------------------
,
----------------------------------------
##################################################

Let's say I need to get only value from the Email field, how can I do this?

    
asked by anonymous 19.06.2016 / 23:44

1 answer

7

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 at IDEONE .

Of course for the specific case you can 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 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 ) );
}

How to use:

$nome  = my_extract( $log, 'Nome:' , ',' );
$email = my_extract( $log, 'Email:', ',' );

See demo on IDEONE .


Using cannon to kill dove:

Since someone was going to end up posting, a solution with RegEx follows:

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.

Once again, check out IDEONE .

If it is to find an occurrence of only the string , I do not recommend it. It sounds simple, but internally the function does a lot of thing more than you need for the proposed problem.

Regular expression description:

 /                 /    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 accented strings in the future, such as "Profession:", and the encoding of your text is multibyte (like UTF-8) for example, instead of strpos use mb_strpos and set your PHP to the correct encoding .

  • As mentioned by colleague @lvcs, if you have a situation where you want to find either Email or EMAIL or eMaIl , you can change strpos to stripos , or mb_strpos by mb_stripos

  • If you really want to ensure that the i string is not confused with something in the middle of the line, you can specify i as a label to include the line break in the search, and the Email: flag to search multiline.

20.06.2016 / 00:01