Jump to content

Archived

This topic is now archived and is closed to further replies.

Buyocat

a regex question

Recommended Posts

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 want
if (eregi($pattern, $string)) echo 'true';
else echo 'false'; // it is only echoing false
[/code]

Share this post


Link to post
Share on other sites
[code]
<?php
if (preg_match('/^[-.\w]+$/', $string)) {
echo 'String is ok';
}
?>
[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
My apologies--I read the description and not the code. Replace the '+' with the same curly notation of '{6,}'.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.