How to show string up to a certain occurrence?

3

I want to display a string up to occurrence X, I could only do it this way:

echo substr($conteudo, 0, strpos($conteudo, 'quebra-de-linha'));

Is there a native resource in PHP that can do this? So I do not need to chain substr and strpos as I did above.

    
asked by anonymous 11.11.2016 / 18:25

1 answer

6

The function strstr has the 3rd parameter, which is used for this:

strstr( $origem, $termo, $flag_somente_antes_do_termo );

Example usage:

strstr( '123456789', '5', true );

Result:

1234

To apply in line break, replace '5' with "\n" or "\r\n" according to the source value.

See the two examples in IDEONE .

    
11.11.2016 / 19:07