Search code examples
phpstrpos

Is there a limit on the length of the needle in the PHP strpos function?


My questions are simple:

  1. Is there a limitation as to what the length of a strpos needle can be?
  2. If so, is there a way to get around it?

It seems like my longer needles don't trigger a position whereas my shorter length ones do.

example:

if (strpos($data, "You were not able to complete this job. Please try again at a later date. Do not hit your back button.") !== false)
//solves as false

if (strpos($data, "You were not able") !== false)
//returns the position

Solution

  • The problem lies elsewhere: while there is likely some implementation limit (e.g. 2GB?), it is not this limit.

    Presumably the longer needle just doesn't match the input:

    • Extra spaces? Remember browsers will "collapse" consecutive HTML whitespace. A "copy'n'paste" from a browser might therefore be different than what cURL reads. (This could also extend to other source not rendered-as-text in a browser.)

    • Things-that-look-like-spaces-but-aren't? (Tabs or newlines?)

    • Or something else that causes the input (and/or needled) to differ from expected ..