Page 1 of 1

strpos()

Posted: Fri Feb 05, 2010 5:22 pm
by joebasolo

Code: Select all

if(strpos($username, $curse) !== false) { echo "<center><font color=#FF0000>No curse words allowed in user name.</font></center>"; }
If I echo $username and $curse I get an answer. $curse resides in $username. I wanted to say basically, if $curse is anywhere in $username, then an error will apear. I'm just doing it this simple for now trying to get the sob to work, but no good! Any help would be appreciated. Thanx!!

Re: strpos()

Posted: Fri Feb 05, 2010 6:33 pm
by AbraCadaver
Depends upon what $curse and $username are. This works:

Code: Select all

$username = 'thecurseman';
$curse = 'curse';
 
if(strpos($username, $curse) !== false) { echo "<center><font color=#FF0000>No curse words allowed in user name.</font></center>"; }

Re: strpos()

Posted: Fri Feb 05, 2010 7:54 pm
by joebasolo
wth, that's what I had. Does not work at all! I don't understand? But, username is a55hole and curse is a55 and it does not pop the warning? That's totally what I had.. My other code all works fine this is just a frustrating one.. does not work.

Re: strpos()

Posted: Fri Feb 05, 2010 7:58 pm
by John Cartwright
Saying does not work doesn't help..

Post some test cases.. with any helpful variable debugging (adding var_dump() for expected values, etc).

P.S., you should consider using the case insensitive stripos()

Re: strpos()

Posted: Fri Feb 05, 2010 8:02 pm
by AbraCadaver
John Cartwright wrote:Saying does not work doesn't help..

Post some test cases.. with any helpful variable debugging (adding var_dump() for expected values, etc).
Yes. Do you get a blank page? Maybe you have a parse error somewhere. This is always good when developing:

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', '1');

Re: strpos()

Posted: Fri Feb 05, 2010 8:08 pm
by joebasolo
ok, tyvm.. will do, but 1st gotta get kids to bed.. thank you again.

Re: strpos()

Posted: Fri Feb 05, 2010 8:11 pm
by AbraCadaver
joebasolo wrote:ok, tyvm.. will do, but 1st gotta get kids to bed.. thank you again.
Aaaaaaaawwwwe, it's Friday. Mine are still up.

Re: strpos()

Posted: Fri Feb 05, 2010 8:13 pm
by joebasolo
ok.. had a quick minute, when ran.. No error with the code added you gave me.. Weird huh? Wonder if it's in a php.ini setting? Just dunno... Sorry to bug ya. My code is the same and no warning display.

Re: strpos()

Posted: Fri Feb 05, 2010 8:23 pm
by joebasolo
ok... EXACT CODE: lol....

Code: Select all

 
    if(isset($username))
    {
        $lines = file("badwords.txt");
        foreach($lines as $curse)
        {
            if(strpos($username, $curse) !== false) { echo "<center><font color=#FF0000>No curse words allowed in user name.</font></center>"; }
        }
    }
 
If I add a new line(s) within the foreach to echo both the username aswell as the curse they show that username is a55hole and curse is a55.... so I really don't know why it is not working. :( Plan on making echo reply a var l8r but for testing purposes this is my code...

Re: strpos()

Posted: Fri Feb 05, 2010 8:35 pm
by redmonkey
Check the PHP function reference for file specifically the use of the 'flags' parameter and the note...
PHP function reference wrote:Note: Each line in the resulting array will include the line ending, unless FILE_IGNORE_NEW_LINES is used, so you still need to use rtrim() if you do not want the line ending present.

Re: strpos()

Posted: Fri Feb 05, 2010 8:45 pm
by joebasolo
ok will do, tyvm!

Re: strpos()

Posted: Fri Feb 05, 2010 8:54 pm
by joebasolo
YOU ROCK! Works flawlessly now!! TYVM!