开发者

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


  1. You haven't shown us the loop. I assume that the code you posted is in the body of the loop
  2. Why use "$variable" ? Quotationmarks are not required here.
  3. You can suppress warnings by writing @functionName();
  4. Check if the needle is empty before applying it
  5. 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜