limitphp Posted February 18, 2009 Share Posted February 18, 2009 I'm using this to check a value: if(!preg_match('#^[a-z0-9](?:[ ]?[a-z0-9])+$#i', $newPlaylist)); //flag The rules are it can start with a letter or number, can contain a space, and end with a letter or number. I'm giving it this value: "Road Songs" and its flagging it. Is my preg_match correct for my rules? thanks Quote Link to comment Share on other sites More sharing options...
amites Posted February 18, 2009 Share Posted February 18, 2009 think your missing adding A-Z in the brackets gave me a match, I see the case insensitive setting though this still is what was needed for it to work for me with your example, might want to try asking in the Regex section Quote Link to comment Share on other sites More sharing options...
Q695 Posted February 18, 2009 Share Posted February 18, 2009 Are you checking if something is in it, if so I would use <?php if ($var){ echo 'do this.'; } ?> Quote Link to comment Share on other sites More sharing options...
limitphp Posted February 19, 2009 Author Share Posted February 19, 2009 think your missing adding A-Z in the brackets gave me a match, I see the case insensitive setting though this still is what was needed for it to work for me with your example, might want to try asking in the Regex section I'm going to change it to just check for [a-z0-9 ] case insensitive before I send it there I will trim it. I'll have a separate check to see if it exists. You shouldn't need to add A-Z if its case insensitive, right? Quote Link to comment Share on other sites More sharing options...
Q695 Posted February 19, 2009 Share Posted February 19, 2009 Why do you need to check it, because the following will allow you to have any values in a string: mysql_real_escape_string($user) Quote Link to comment Share on other sites More sharing options...
limitphp Posted February 19, 2009 Author Share Posted February 19, 2009 Why do you need to check it, because the following will allow you to have any values in a string: mysql_real_escape_string($user) Because, the playlist name will eventually be used as part of a url: ex) mysite.com/userplaylists/username/playlistnameHash/ I have to make sure it only contains letters,numbers and spaces. All other characters would not be SEO friendly. except for underscores. But why allow underscores when dashes will do just as well. Quote Link to comment Share on other sites More sharing options...
limitphp Posted February 19, 2009 Author Share Posted February 19, 2009 Ok, I redid the routines..... <?php $newPlaylist = trim($newPlaylist); $newPlaylist = preg_replace('/\\s{2,}/',' ',$newPlaylist); //Replace multiple spaces in between characters with 1 space if(!preg_match('#^[a-zA-Z0-9 ]+$#i', $newPlaylist)); //Check for invalid characters //flag I set $newPlaylist= "Rock Indie" its flagging it.... that shouldn't be happening, right? I'm allowing a-z A-Z and 0-9 and spaces....how is this happening? Quote Link to comment Share on other sites More sharing options...
limitphp Posted February 19, 2009 Author Share Posted February 19, 2009 here's the problem: if(!preg_match('#^[a-zA-Z0-9 ]+$#i', $newPlaylist)); I left the semicolon after the if statement..... nevermind...apparently that didn't fix it....it still won't let "Rock Indie" through....its flagging it.... Quote Link to comment Share on other sites More sharing options...
limitphp Posted February 19, 2009 Author Share Posted February 19, 2009 ok, this flags "Rock Indie" if(!preg_match('#^[a-zA-Z0-9 ]?$#i', $newPlaylist)) //flag this does not flag "Rock Indie", but it requires a value: if(!preg_match('#^[a-zA-Z0-9 ]+$#i', $newPlaylist)) //flag Why does the question mark flag "Rock Indie"? In the tutorial, it says the question mark allows the pattern 0 or more times....what am I misunderstanding? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.