简体   繁体   English

php - 如何摆脱这个奇怪的“空分隔符”消息

[英]php - How do I get rid of this strange “empty delimiter” message

I have some code that uses the stristr function to extract data I need. 我有一些代码使用stristr函数来提取我需要的数据。

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 基本上只有当$ key(针)不是空字符串时才执行stristr

  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" ? 为什么要使用“$ variable”? Quotationmarks are not required here. 这里不需要引号。
  3. You can suppress warnings by writing @functionName(); 您可以通过编写@functionName()来禁止警告;
  4. Check if the needle is empty before applying it 在使用之前检查针是否为空
  5. HTML (< BR>) should be lowercase HTML(<BR>)应为小写

Quote from php.net stristr user: dpatton.at.confluence.org 引自php.net stristr用户: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. PHP 4.2.3中的更改可能导致在使用stristr()时生成警告消息,即使在旧版本的PHP中没有生成消息。

The following will generate a warning message in 4.0.6 and 4.2.3: 以下将在4.0.6和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: 不会在4.0.6中生成“空分隔符”警告消息,但在4.2.3中生成:

unset($needle); 
stristr("haystack", $needle);

Here's a URL that documents what was changed 这是一个记录更改内容的URL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM