strpos()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
joebasolo
Forum Newbie
Posts: 7
Joined: Fri Feb 05, 2010 5:18 pm

strpos()

Post 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!!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: strpos()

Post 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>"; }
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
joebasolo
Forum Newbie
Posts: 7
Joined: Fri Feb 05, 2010 5:18 pm

Re: strpos()

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: strpos()

Post 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()
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: strpos()

Post 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');
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
joebasolo
Forum Newbie
Posts: 7
Joined: Fri Feb 05, 2010 5:18 pm

Re: strpos()

Post by joebasolo »

ok, tyvm.. will do, but 1st gotta get kids to bed.. thank you again.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: strpos()

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
joebasolo
Forum Newbie
Posts: 7
Joined: Fri Feb 05, 2010 5:18 pm

Re: strpos()

Post 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.
joebasolo
Forum Newbie
Posts: 7
Joined: Fri Feb 05, 2010 5:18 pm

Re: strpos()

Post 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...
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Re: strpos()

Post 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.
joebasolo
Forum Newbie
Posts: 7
Joined: Fri Feb 05, 2010 5:18 pm

Re: strpos()

Post by joebasolo »

ok will do, tyvm!
joebasolo
Forum Newbie
Posts: 7
Joined: Fri Feb 05, 2010 5:18 pm

Re: strpos()

Post by joebasolo »

YOU ROCK! Works flawlessly now!! TYVM!
Post Reply