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
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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

 

 

Link to comment
Share on other sites

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?

 

 

 

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.