Buyocat Posted July 31, 2006 Share Posted July 31, 2006 I'm trying to do a really simple regular expression, but it doesn't seem to be working. I want to check that a string contains only numbers, letters and the "_.-" characters. (Obviously they don't have to be in a string like that) Anyway here is what I have created, the problem it is it never evaluates properly -- it just evaluates to false regardless of what I feed it.[code]$pattern = '[^a-zA-Z0-9_.-]{6,}'; // I want it to be at least 6 characters in length$string = 'helloworld'; // pick any string you wantif (eregi($pattern, $string)) echo 'true';else echo 'false'; // it is only echoing false[/code] Quote Link to comment Share on other sites More sharing options...
effigy Posted July 31, 2006 Share Posted July 31, 2006 [code]<?php if (preg_match('/^[-.\w]+$/', $string)) { echo 'String is ok'; }?>[/code] Quote Link to comment Share on other sites More sharing options...
Buyocat Posted July 31, 2006 Author Share Posted July 31, 2006 Thank you for the reply, but I have one more concern. How can I specify a string length? It appears to accept a string of any length at the moment. Quote Link to comment Share on other sites More sharing options...
effigy Posted July 31, 2006 Share Posted July 31, 2006 My apologies--I read the description and not the code. Replace the '+' with the same curly notation of '{6,}'. 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.