1

I have a string and an array, the array contains all kinds of parts of a string I want to find in the original string (this is for basically reading a error log and identifying what line there is a "Could not find", or "Error", etc.)

Is the foreach preg_match the best method?

1 Answer 1

2

The easiest and speediest way is using strpos(). If it returns FALSE it didn't find the substring, otherwise it did. Make sure you use === as it might return 0:

$found_substring = (strpos($text, $substring) !== FALSE);

For case-insensitivity, use stripos(). If you need more matching power, use preg_match().

Sign up to request clarification or add additional context in comments.

1 Comment

So the best method would be to loop through the array and do strpos for each array key?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.