Jump to content

Need Help Checking Value


limitphp

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/145816-need-help-checking-value/
Share on other sites

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?

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.

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?

 

 

 

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?

 

 

 

 

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.