php-general Digest 16 Mar 2011 11:44:50 -0000 Issue 7229

Topics (messages 311881 through 311884):

Re: String eval assistance
        311881 by: Simon J Welsh
        311882 by: Jack
        311883 by: Richard Quadling
        311884 by: Alex

Administrivia:

To subscribe to the digest, e-mail:
        [email protected]

To unsubscribe from the digest, e-mail:
        [email protected]

To post to the list, e-mail:
        [email protected]


----------------------------------------------------------------------
--- Begin Message ---
On 16/03/2011, at 10:34 AM, Jack wrote:

> Hello All,
> 
> 
> 
> I got some help on this yesterday, but somehow it's not consistant
> 
> 
> 
> <?
> 
> 
> 
> $results = "3434approd34";
> 
> 
> 
> if(strpos($results['response'], 'APPROVED') !== false) {
> 
> 
> 
>   print "declined";
> 
> 
> 
> } else {
> 
> 
> 
>   print "approved";
> 
> }
> 
> 
> 
> ?>
> 
> 
> 
> 
> 
> The thing is I cant get a consistant response, if it has approved anywhere
> in the results string, then it should be approved and if the results is
> APPROVD without the E it shold be delined.
> 
> 
> 
> Am I doing something wrong.
> 
> 
> 
> 
> 
> Thanks!
> 
> Jack

Yes, you're doing something wrong. strpos() returns false if it can't find the 
needle. You should be using if(strpos() === false) { declined; }
---
Simon Welsh
Admin of http://simon.geek.nz/

Who said Microsoft never created a bug-free program? The blue screen never, 
ever crashes!

http://www.thinkgeek.com/brain/gimme.cgi?wid=81d520e5e


--- End Message ---
--- Begin Message ---
>     Here you're trying to access it as an array, which it's not, so the
'response'
> key doesn't exist.  In addition, you're looking for UPPER-CASE, whereas
that's
> not the case in your example variable.
> Finally, you're checking to make sure that the string IS INDEED found, but
> then printing that it was declined (!== false).  Instead, you may
> want:
> 
> <?php
> 
> $results['response'] = '3434approd34';
> 
> if (stripos($results['response'],'APPROVED') !== false) {
>     // It's been found
> } else {
>     // Oh, crap.
> }
> 
> ?>

maybe I should do this some other way because I'm getting false positives.

I was using if(strpos($results['response'], 'APPROVED') !== false) {
And its found if the value of $results = "3434APPROVED34" and it also is
found if its $results = "3434APPOVED34", so this may not be the best way to
accomplish this.


--- End Message ---
--- Begin Message ---
On 16 March 2011 00:25, Jack <[email protected]> wrote:
>>     Here you're trying to access it as an array, which it's not, so the
> 'response'
>> key doesn't exist.  In addition, you're looking for UPPER-CASE, whereas
> that's
>> not the case in your example variable.
>> Finally, you're checking to make sure that the string IS INDEED found, but
>> then printing that it was declined (!== false).  Instead, you may
>> want:
>>
>> <?php
>>
>> $results['response'] = '3434approd34';
>>
>> if (stripos($results['response'],'APPROVED') !== false) {
>>     // It's been found
>> } else {
>>     // Oh, crap.
>> }
>>
>> ?>
>
> maybe I should do this some other way because I'm getting false positives.
>
> I was using if(strpos($results['response'], 'APPROVED') !== false) {
> And its found if the value of $results = "3434APPROVED34" and it also is
> found if its $results = "3434APPOVED34", so this may not be the best way to
> accomplish this.
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Can you create a small list of actual values and their results.

What version of PHP are you using?



-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

--- End Message ---
--- Begin Message ---
I'm not sure as to why strpos does what it does here, at least its not 
immediately obvious, but, a solution to this would be to use a regular 
expression search, it would be more exact, it has never failed me, and it will 
be faster; I recall reading that preg functions were faster at then str ones, 
though I can't recall where...
-- 
Sent from my Android phone with K-9 Mail. Please excuse my brevity.

Richard Quadling <[email protected]> wrote:

On 16 March 2011 00:25, Jack <[email protected]> wrote: >>     Here you're 
trying to access it as an array, which it's not, so the > 'response' >> key 
doesn't exist.  In addition, you're looking for UPPER-CASE, whereas > that's >> 
not the case in your example variable. >> Finally, you're checking to make sure 
that the string IS INDEED found, but >> then printing that it was declined (!== 
false).  Instead, you may >> want: >> >> <?php >> >> $results['response'] = 
'3434approd34'; >> >> if (stripos($results['response'],'APPROVED') !== false) { 
>>     // It's been found >> } else { >>     // Oh, crap. >> } >> >> ?> > > 
maybe I should do this some other way because I'm getting false positives. > > 
I was using if(strpos($results['response'], 'APPROVED') !== false) { > And its 
found if the value of $results = "3434APPROVED34" and it also is > found if its 
$results = "3434APPOVED34", so this may not be the best way to > accomplish 
this. > > > -- > PHP General Mailing List
(http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > 
Can you create a small list of actual values and their results. What version of 
PHP are you using? -- Richard Quadling Twitter : EE : Zend @RQuadling : 
e-e.com/M_248814.html : bit.ly/9O8vFY -- PHP General Mailing List 
(http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php 


--- End Message ---

Reply via email to