tags:

views:

81

answers:

3

I have some code that uses the stristr function to extract data I need. It works, in that it gives me the results I'm looking for. BUT (you knew there was a but), it gives me this error message for every iteration of the loop:

Warning: stristr() [function.stristr]: Empty delimiter in ... line 55

Like I said, the code works apart from this error. Can anyone suggest how i could amend this code to get rid of the message? Thanks in advance

$data = stristr("$text", "$key");
$result = string_limit_words($data,2);
print "$result<BR>";
+2  A: 
$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

Bob Fincheimer
Thanks. I believe there was one empty $key that spoiled it for the rest!
Steven
+1  A: 
  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
Jan Kuboschek
I'm sorry, why the downvote?
Jan Kuboschek
-1 you didn't answer his question, suggested suppressing warnings as the solution, and nagged about a bunch of other stuff that wasn't central to the problem.
Steven Oxley
Edited. The rest is FYI.
Jan Kuboschek
@Steven Oxley: Actually, he did answer the question at point #4.
animuson
Well, number 4 is indeed the problem. and empty (as in "" ) will throw this error. Null will return false, incidentally. (jan, just make number 4 number one, eh?)
Dan Heberden
To be fair, point 4 turned out to be the solution
Steven
@Dan Heberden: Which would also make #2 correct, because the quotes make it an empty string instead of NULL (unless the variable is specifically defined to be an empty string before this).
animuson
@animuson I believe he added that after my downvote.
Steven Oxley
@Steven Oxley I was in the process of editing while you were downvoting. :)
Jan Kuboschek
@Jan Kuboschek OK, OK, I didn't realize and I have attempted to remove my downvote, but apparently you made the edit after my downvote got processed (in other words, while I was reading the answer) because it says I can't change my vote unless the answer is edited.
Steven Oxley
No problem, someone upvoted it :)
Jan Kuboschek
@animuson - good point! - and i bet you that was his issue, too
Dan Heberden
+1  A: 

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: http://groups.google.ca/groups?selm=cvshholzgra1031224321%40cvsserver

Neb
Consulted the manual already and it didn't help, but thanks
Steven