Regex Scripting

I’m very new to php, but I think I’m picking it up.

I’m trying to ban some characters from being enterred in a text field.

I don’t understand regex at all, I have a script, but it doesn’t seem to work and I’m not sure why. I found a helpful topic on this site and thought you guys may be able to help me finish my quest.

So, restrictions:
-Cannot start with a #
-no special characters (only a-z, A-Z and #'s)
-only single spaces

Is this correct?


mmh no. You’re close though. 2/3 isnt bad.
What you’ve got there says
“Starts with a letter (either case), then any number of word characters and spaces.”
This doesnt exclude someone from using multiple spaces in a row.

How would I add that in?

sorry, semi-here today due to work or i’d have put it in my first post.

I’m not an PCRE expert, but to me, the appropriate value is…


Note: This will not allow a space character at the end of the string.

  1. a-zA-Z ↩︎

I don’t see anything to allow # after the first character.


Wouldn’t that be a little closer? I also added a dollar sign, because without it, something like: "Aa 32532 36326 362 " could match.

Also, I switched the * to a +, which you’ll want to use if it requires them to have at least 2 characters in the pattern. Leave it a * if one character is sufficient. If you have a valid range of characters, you could also specify that instead of a * or + (say if the length had to be between 10 and 20, you can replace the + with {9,19}, minus one because of the first character).

  1. a-zA-Z ↩︎

Alright, I got that sorted, but now have a few more issues.

I’m trying to search for any numbers 0 and above.


Is that right for a regex code? ^^

if (!preg_match_all( "/^[0-9]+$/", $card, $gtdump ))
				{ //found recent games
				{ //cant find recent games
					echo 'Recent Games not avaliable';

Thats the script, but its returning an empty array. I used file_get_contents, so I’m not sure what the problem is.

The print_r exports

Array ( [0] => Array ( ) )

Which is an empty array, correct?

So, you say:


The ! means “not”, so you’re saying “If it’s not this, print it, else say nothing is available”. Remove your ! and you should be good to go, the regex looks good.

Alright, I got that all sorted out, but then realised I needed to change the regex because I was getting to many results.

Now I’m trying to do the same thing, but search through a div tag as well, thus lessening the results to 1 (instead of 90 previously).

if(preg_match_all( "/<div class=\\"Gamerscore\\">([0-9]+)<\\/div>/", $card, $gtdump))

I’m not sure the proper syntax on something with div tags, when googled I got something like that.

Now it is only returned my else statement, so its not picking up any results.