/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] reg expression check

I want to check that a value passed to my script ONLY contains a to z, 0 to 9 or _. That means no spaces.
This seems to work but I am new to this so want to check.

$string = ‘hello hello’;
ereg(‘^[a-z0-9_]+[a-z0-9_]+$’, $string, $array);
returns false.

When I had this
ereg(‘^[a-z0-9_]+$’, $string, $array);
It was happy and returned the string in array[0].

Confused.

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiDec 11.2009 — The only difference between the two expressions you posted is that the first one will fail if the string has only one character. That's because it says "match the start of the string, followed by a-z, 0-9 or_ one or more times. Match the same thing again one or more times, then match the end of the string.

A couple of points which may help:

The pattern you want to use is so common it has its own special "code" in regular expressions lingo - w.

If you really do want to have at least 2 characters you are better off with the following:

^w{2,}$

The braces identify repetition limits; you can supply an exact number of repetitions, or an upper and lower limit. For example:

{2} = repeat exactly twice

{2,4} = repeat between 2 and 4 times inclusive

{2,} = repeat at least twice (no upper limit)

{,4} = repeat no more than 4 times (no lower limit}

Another thing to consider is case sensitivity - i'll get to that in a second.

I would also avoid using ereg as it is depreciated, you should use preg_match instead. So assuming you do want to match only lowercase a-z 0-9 and _ one or more times, I would use the following:

[code=php]if (preg_match('/w+/', $string)) {
// match
}[/code]


If you need it case insensitive, you can add the i flag to the expression:

[code=php]if (preg_match('/w+/i', $string)) {
// match
}[/code]


As a last example just for completeness, if you want to match at least 2 letters in a case insensitive manner:

[code=php]if (preg_match('/w{2,}/i', $string)) {
// match
}[/code]
Copy linkTweet thisAlerts:
@SrWebDeveloperDec 11.2009 — Excellent tutorial, Mindzai.

-jim
Copy linkTweet thisAlerts:
@NogDogDec 11.2009 — ...

The pattern you want to use is so common it has its own special "code" in regular expressions lingo - w.

...
[/QUOTE]

It's not quite the same, as "w" includes both upper- and lower-case letters, and may be locale-dependent as to what it considers to be a letter (or a number for that matter):
[code=php]
$test = "a2²B3³";
var_dump(preg_match('#^w+$#', $test)); // (int)1
[/code]
Copy linkTweet thisAlerts:
@MindzaiDec 11.2009 — My mistake, I forgot about the uppercase letters :o. I didn't realise it would match superscript numbers either (though it makes sense I suppose). Thanks for the clarification ?
Copy linkTweet thisAlerts:
@NogDogDec 11.2009 — I never even considered the possibility of the superscript 2 or 3 characters until someone showed that on another forum. Seems a bit odd to me, as I'll bet 99.99% of the time when people use "d" or "w" they do not, in fact, want to include them. :rolleyes:
Copy linkTweet thisAlerts:
@MindzaiDec 11.2009 — I suppose it would be a pain to match them otherwise though - can you specify a unicode range? Are they even consecutive codes? I guess if so it would be OK, but otherwise a bit of a nightmare.
Copy linkTweet thisAlerts:
@suejohauthorDec 14.2009 — Thanks for the input. Will look at preg_match. However the suggested pattern does not work for me as the pattern must ensure there are no spaces and this one accepts spaces.

the pattern must show this as invalid - 'a dog'

and this would be valid 'adog_'

Sue
Copy linkTweet thisAlerts:
@NogDogDec 14.2009 — Which pattern did you use? This should work:
[code=php]
preg_match('/^[a-z0-9_]+$/', $string)
[/code]

Or if you want at least 2 characters:
[code=php]
preg_match('/^[a-z0-9_]{2,}$/', $string)
[/code]
Copy linkTweet thisAlerts:
@Stephen_PhilbinDec 14.2009 — Suejoh. Do you want to allow only lower-case letters, do you want to allow letters of any case?

If you want both upper- and lower-case then you'll need to either use the "i" modifier (as seen above), or specify both cases. [code=php]"/[a-zA-Z0-9_]{2,}/"[/code] for example, will accept "Stephen", but most of the expressions seen so far will not. Remember to think about case-sensitivity whenever you're forming and testing regular expressions.

I think you could also use [code=php]"[[alphanum]]"[/code] for the expression too, but I'm not sure if this includes the underscore character and whether or not its meaning is locale-dependent.
Copy linkTweet thisAlerts:
@suejohauthorDec 14.2009 — Thanks for that.

So preg_match uses the same patterns as ereg but just puts them between / /.

If that is true what is the difference between them?

I suppose not to worry for now.

Too much to learn!

Sue
Copy linkTweet thisAlerts:
@suejohauthorDec 14.2009 — Used this ^[[:alnum:]]*$

and it was happy with A-Za-z0-9 but pattern match failed with

spaces and other char such as _

Also - I did want to exclude upper case so that was fine - thaks for pointing it out.

Also very helpful is the bit about {n,m} - so can specify number of char.

Thaks a lot everyone for your time.

Sue
×

Success!

Help @suejoh spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...