Jump to content

strpos nightmare


george
 Share

Recommended Posts

I have this simple little function. I pass to it a string such as canc,crue,medi The function is supposed to echo out what these 4 letter words stand for, for all of them in the list. I will get results for everything except the first four letter item in the list. for instance, the string above will returnCruelty-Free Charities: for crueMedical School Live-Animal Labs: for medi but not Cancer Prevention: A Message from . . . for canc, the first four letter word in the list. it acts as though canc was not contained within canc,crue,medi. Why?!?

function listem($vPSA) {$Video='';if (!strpos($vPSA, "crue")===false) {$Video .= '<li>Cruelty-Free Charities: Humane Seal of Approval (Noah Wyle)</li><br />';}if (!strpos($vPSA, "vdel")===false) {$Video .= '<li>Vegetarian Diet: El Poder de la Alimentación</li><br />';}if (!strpos($vPSA, "canc")===false) {$Video .= '<li>Cancer Prevention: A Message from Anthony Hopkins</li><br />';}if (!strpos($vPSA, "medi")===false) {$Video .= '<li>Medical School Live-Animal Labs: Don’t Let Your Medical Education Go to the Dogs (Bill Maher)</li><br />';}echo $Video;}

I also tried )>0) instead of ! ===false. I know the string is recieved properly by the function because I can echo it to the screenHow do I tell it to see the first part of the string? I am going to go bang my head aginst the wall some more now.

Link to comment
Share on other sites

I have this simple little function. I pass to it a string such as canc,crue,medi The function is supposed to echo out what these 4 letter words stand for, for all of them in the list. I will get results for everything except the first four letter item in the list. for instance, the string above will returnCruelty-Free Charities: for crueMedical School Live-Animal Labs: for medi but not Cancer Prevention: A Message from . . . for canc, the first four letter word in the list. it acts as though canc was not contained within canc,crue,medi. Why?!?
function listem($vPSA) {$Video='';if (!strpos($vPSA, "crue")===false) {$Video .= '<li>Cruelty-Free Charities: Humane Seal of Approval (Noah Wyle)</li><br />';}if (!strpos($vPSA, "vdel")===false) {$Video .= '<li>Vegetarian Diet: El Poder de la Alimentación</li><br />';}if (!strpos($vPSA, "canc")===false) {$Video .= '<li>Cancer Prevention: A Message from Anthony Hopkins</li><br />';}if (!strpos($vPSA, "medi")===false) {$Video .= '<li>Medical School Live-Animal Labs: Don’t Let Your Medical Education Go to the Dogs (Bill Maher)</li><br />';}echo $Video;}

I also tried )>0) instead of ! ===false. I know the string is recieved properly by the function because I can echo it to the screenHow do I tell it to see the first part of the string? I am going to go bang my head aginst the wall some more now.

Have you checked the code that calls this function? I think you may find the problem there. Not knowing the details of how you get the value from your comma-separated list, perhaps you are doing a loop starting from 1 instead of 0, or that kind of thing, missing the first value, so your function is never called with "canc".Try hard-coding a call to the function passing the value "canc", to test your function in isolation from the calling code.
Link to comment
Share on other sites

Good suggestions. I echo the passed string both before the function is called, and from within the function itself. And so I know this part is good. And I never use a for or while loop to parce it. I don't need to parce it. I just need to know if a little string exists within a big string, and if it does, append a value to another string.

Link to comment
Share on other sites

Good suggestions. I echo the passed string both before the function is called, and from within the function itself. And so I know this part is good. And I never use a for or while loop to parce it. I don't need to parce it. I just need to know if a little string exists within a big string, and if it does, append a value to another string.
The negation is getting evaluated at the wrong place. Use !== instead of !strpos:
if (strpos($vPSA, "crue")!==false) ...

Link to comment
Share on other sites

I got it. I needed to use if (strpos($vPSA,"cp_prog")!==false) instead of if (!strpos($vPSA, "crue")===false) or if (strpos($vPSA, "crue")>=0) Why these two failed in this particular way I do not know. But the top one works.

Link to comment
Share on other sites

I got it. I needed to use if (strpos($vPSA,"cp_prog")!==false) instead of if (!strpos($vPSA, "crue")===false) or if (strpos($vPSA, "crue")>=0) Why these two failed in this particular way I do not know. But the top one works.
if (!strpos($vPSA, "crue")===false)Fails because what you're telling the PHP parser there is"return the opposite of strpos returns, and if that is false, then do this."The second shouldn't be failing. It's possible you were using bad computer logic and kind of inversed the operation you were trying to do.
Link to comment
Share on other sites

It's easier to see with parentheses. The way you had it, like this:if (!strpos($vPSA, "crue")===false)The computer is reading like this:if ( (!strpos($vPSA, "crue")) ===false)The negation is being applied to the return value of strpos, and then you are testing if that negated value is false. So if strpos returns true (or a non-zero number) then you are negating that to get false, and then comparing that with the value false. That will work for everything that doesn't start the string, but if strpos returns a 0 indicating that the item shows up at the beginning of the string then you negate the 0 to get true, and then compare true with false, so it fails the test.

Link to comment
Share on other sites

Still mastering this language, but wouldn't preg_match be (slightly) more efficient than testing against false? Like this, I mean:if (preg_match("/crue/", $vPSA)) {}More intuitive, anyway.
According to the manual, regular expressions are slower than single functions for a specific task, because the expression needs to be evaluated by the complete regex library, whereas a specialized function just does a single (or a few) internal operations to return the result.I do agree it's more intuitive though.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...