在PHP中搜索非空白正确的字符串

I'm have a project where I need to find occurrences of a string in a large body of text. The search string is known to be present in the larger text, however for reasons beyond my control they are not white-space correct, in that they are missing spaces between some of the words.

For example I the string to find is (not the lack of space between brown and fox:

quick brownfox jumps

And I need to find this in:

The quick brown fox jumps over the lazy dog.

I need to be able to modify the haystack to wrap the found terms with an identifying tag so I'll end up with something like:

The <span class="found">quick brown fox jumps</span> over the lazy dog.

I've looked into using regex in free-spacing mode which seems to not quite do what I need, I considered stripping all white space from the search terms and adding \s* between each character but thought this might have a horrendous effect on performance (can any regex experts could confirm or deny that?).

Are there any possible non-regex solutions to look into.

Thanks

The best way in this case would be to remove all the whitespaces in the search string, and the target string. And then check if the string is present or not:

$haystack = 'The quick brown fox jumps over the lazy dog.';
$needle = 'quick brownfox jumps';

$haystack = preg_replace("\s+", "", $haystack);
$needle = preg_replace("\s+", "", $needle);

if (strpos($haystack, $needle) !== false) {
    echo 'true';
}

You can't just strip the whitespace from the haystack like others are saying. Your search string, even though its whitespace is unreliable, is still a series of discrete words. If we assume that the whitespace is correct in your haystack, that means your string to find will be be surrounded by non-word characters in the haystack. By stripping it of whitespace, you are losing the ability to check for that and you'll get unnecessary false positives.

Something like \Ws\W*t\W*r\W*i\W*n\W*g\W would work, but it isn't very clean. If you want to implement a solution without using regular expressions, you could iterate over every word in the haystack and compare it to the first n characters of your search string, then try to match proceeding words with the rest of the search string. Once you get to a character that doesn't match, you skip the rest of the word and start checking the next one. It only returns a complete match if your search string's last character matches with the end of a word in your haystack.