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] Link to comment https://forums.phpfreaks.com/topic/16113-a-regex-question/ 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] Link to comment https://forums.phpfreaks.com/topic/16113-a-regex-question/#findComment-66421 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. Link to comment https://forums.phpfreaks.com/topic/16113-a-regex-question/#findComment-66432 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,}'. Link to comment https://forums.phpfreaks.com/topic/16113-a-regex-question/#findComment-66455 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.