Jump to content


Photo

a regex question


  • Please log in to reply
3 replies to this topic

#1 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 31 July 2006 - 03:50 PM

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.

$pattern = '[^a-zA-Z0-9_.-]{6,}'; // I want it to be at least 6 characters in length
$string = 'helloworld'; // pick any string you want
if (eregi($pattern, $string)) echo 'true';
else echo 'false'; // it is only echoing false

Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 31 July 2006 - 03:55 PM

<?php
	if (preg_match('/^[-.\w]+$/', $string)) { 
		echo 'String is ok';
	}
?>

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 31 July 2006 - 04:12 PM

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.
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#4 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 31 July 2006 - 04:50 PM

My apologies--I read the description and not the code. Replace the '+' with the same curly notation of '{6,}'.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users