0

I am trying to write a regex that will only allow text uppercase and lower. I had no idea how to write any regex so I found a post on Stack

How do I write a regex in PHP to remove special characters?

With a link to a place to learn. The tut is fantastic so do read if yuo have trouble with regex.

if( preg_match('/^([A-Za-z\s]+)&/', $pagename) ) {
 //do stuff
 } 
 else  
 {echo"no special char or num";}

How ever it fails. If I type for example

New Web Page

it should allow this but it fails.

Community
  • 1
  • 1
Daniel Robinson
  • 643
  • 1
  • 10
  • 25
  • fyi \s matches for a "whitespace" character (eg: a space, tab, etc.). If you really want to only match letters, you need to remove that. Also, you can shorten the regex by using the i modifier. – CrayonViolent Jan 28 '13 at 16:20
  • are you sure the $pagename contains the correct value that you are parsing? – TheTechGuy Jan 28 '13 at 16:22

3 Answers3

1

$ is the right sign for ends with not &

it should be /^([A-Za-z\s]+)$/

t.niese
  • 39,256
  • 9
  • 74
  • 101
1

I am trying to write a regex that will only allow text uppercase and lower. All you need is

 '/^([A-Z]+)$/i'
            ^
            |-------- Note used $ instead of &
Baba
  • 94,024
  • 28
  • 166
  • 217
0

You don't need the round brackets, and replace & with $ (end of string).

ChrisIPowell
  • 494
  • 2
  • 6
  • He needs the brackets to show that a-z and A-Z are character sets – user387049 Jan 28 '13 at 16:18
  • I suspect he means the parentheses rather than the brackets - technically the parentheses are unnecessary unless you want to create/store a sub-pattern. – CD001 Jan 28 '13 at 16:24
  • 1
    `()`, `[]`, `{}` - are these A) (round) brackets, square brackets, curly brackets, or B) parens, brackets, braces? :-) – melpomene Jan 28 '13 at 16:24
  • OP's looking for a true or false, so he needs the square brackets but not the round ones so `/^[a-zA-Z\s]+$/` would do exactly what he wants. – ChrisIPowell Jan 28 '13 at 16:26
  • Yes, brackets means (), should've been clearer I meant those and not the Square brackets []! – ChrisIPowell Jan 28 '13 at 16:27