How to show string to a given occurrence?

Asked

Viewed 51 times

3

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

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

In PHP is there a native resource that can do this? So I don’t need to string subst and strpos as I did above.

  • Your question is not clear, could you put an example of a string initial and what should be returned from it?

1 answer

6


The function strstr has the third parameter, which serves for this:

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

Example of use:

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

Upshot:

1234

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

See the two examples in IDEONE.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.