Simple ereg help | | |
Hi,
I need two ereg expressions in my PHP code. One of them needs to check
that a string only contains letters, and the other needs to check that
the string only contains letters and commas (only one comma at each
time).
I thought that the code for only containing letters would be:
eregi("^([a-z])", $keywords);
But this only appears to be checking the first character.
I also have no idea how to do the one that also allows commas (though
not for lack of trying - believe me!)
So, basically, I want one expression that only lets in letters:
ie. hyasdlhlasdhl but not fhdilfd7800asdads;'
and one expression that only lets in letters and commas (one at a
time):
ie. hasiaks,asdas,adsads but not hdasl,,ahodsa,ads
Thanks alot. | | | | re: Simple ereg help ollie.mitch@gmail.com wrote: Quote:
Hi,
>
I need two ereg expressions in my PHP code. One of them needs to check
that a string only contains letters, and the other needs to check that
the string only contains letters and commas (only one comma at each
time).
>
I thought that the code for only containing letters would be:
>
eregi("^([a-z])", $keywords);
Use the preg-variant, it's faster.
preg_match('/^[a-z]+$/i',$text); Quote:
and one expression that only lets in letters and commas (one at a
time):
ie. hasiaks,asdas,adsads but not hdasl,,ahodsa,ads
preg_match('/^[a-z]+(,[a-z]+)*?$/i',$text(;
Grtz,
--
Rik Wasmus | | | | re: Simple ereg help ollie.mitch@gmail.com wrote: Quote:
Hi,
>
I need two ereg expressions in my PHP code. One of them needs to check
that a string only contains letters, and the other needs to check that
the string only contains letters and commas (only one comma at each
time).
>
I thought that the code for only containing letters would be:
>
eregi("^([a-z])", $keywords);
>
But this only appears to be checking the first character.
>
I also have no idea how to do the one that also allows commas (though
not for lack of trying - believe me!)
>
>
So, basically, I want one expression that only lets in letters:
ie. hyasdlhlasdhl but not fhdilfd7800asdads;'
>
eregi("/^[a-z]*$/",$keywords);
* null or more
+ one or more Quote:
and one expression that only lets in letters and commas (one at a
time):
ie. hasiaks,asdas,adsads but not hdasl,,ahodsa,ads
>
eregi("[a-z]+,{1}",$keywords); | | | | re: Simple ereg help ollie.mitch@gmail.com: Quote:
I need two ereg expressions in my PHP code. One of them needs to check
that a string only contains letters,
'letters' means A-Z?
(untested)
preg_match('/\A[a-z]+\z/i',$subject)
Meaning: match, starting at the beginning and finishing at the end,
one or more letters, case insensitive. Quote:
and the other needs to check that the string only contains
letters and commas (only one comma at each time).
(untested)
preg_match('/\A(,(?!,)|[a-z]*)+\z/i',$subject)
Meaning: match, one or more times, a comma that is not followed by
another comma or match zero or more letters. Quote:
I thought that the code for only containing letters would be:
>
eregi("^([a-z])", $keywords);
>
But this only appears to be checking the first character.
Yeah, you haven't quantified the character class, e.g., /^[a-z]+/, or
anchored the pattern to the end of the subject, e.g., /^[a-z]+$/.
--
Jock | | | | re: Simple ereg help ollie.mitch@gmail.com wrote: Quote:
So, basically, I want one expression that only lets in letters:
ie. hyasdlhlasdhl but not fhdilfd7800asdads;'
Would you consider using preg_* instead of ereg?
Is "ñ" a letter? Is "" a string containing only letters?
Assuming 'yes' to both questions, try
if (preg_match('/^[[:alpha:]]*$/', $variable)) {
echo 'All letters.';
} else {
echo 'Not all letters.';
} Quote:
and one expression that only lets in letters and commas (one at a
time):
ie. hasiaks,asdas,adsads but not hdasl,,ahodsa,ads
Can the string start or end with a (single) comma?
Assuming 'no', try
if (preg_match('/^[[:alpha:]]+(?:,[[:alpha:]]+)?$/', $variable)) {
echo 'Match';
} else {
echo 'No match.';
}
--
File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,366 network members.
|