php - How do I get rid of this strange "empty delimiter" message
I have some code tha开发者_Python百科t uses the stristr function to extract data I need.
It gives me this Warning for every iteration of the loop:
Warning: stristr() [function.stristr]: Empty delimiter in ... line 55
The code works apart from this Warning. Here is the code:
$data = stristr("$text", "$key");
$result = string_limit_words($data,2);
print "$result<BR>";
How do I get rid of the warning message?
$data = $text;
if($key)
$data = stristr($data, $key);
$result = string_limit_words($data,2);
print "$result<BR>";
Basically only do the stristr if the $key (the needle) is not an empty string
- You haven't shown us the loop. I assume that the code you posted is in the body of the loop
- Why use "$variable" ? Quotationmarks are not required here.
- You can suppress warnings by writing @functionName();
- Check if the needle is empty before applying it
- HTML (< BR>) should be lowercase
Quote from php.net stristr user: dpatton.at.confluence.org
There was a change in PHP 4.2.3 that can cause a warning message to be generated when using stristr(), even though no message was generated in older versions of PHP.
The following will generate a warning message in 4.0.6 and 4.2.3:
stristr("haystack", "");
OR
$needle = "";
stristr("haystack", $needle);
This will not generate an "Empty Delimiter" warning message in 4.0.6, but will in 4.2.3:
unset($needle);
stristr("haystack", $needle);
Here's a URL that documents what was changed
精彩评论