473,386 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.

Oct 19 '06 #1
4 1833
Rik
ol*********@gmail.com wrote:
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);
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
Oct 19 '06 #2
ol*********@gmail.com wrote:
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
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);
Thanks alot.
Oct 19 '06 #3
ol*********@gmail.com:
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.
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.
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

Oct 19 '06 #4
ol*********@gmail.com wrote:
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.';
}
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
Oct 19 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: bonehead | last post by:
Greetings, I'd like to add somee error handling to a password field: if the password is fewer than 5 characters or greater than 15 characters I'd like to trap it. Special characters such as ^...
1
by: Stefan Gangefors | last post by:
I'm trying to figure out what I'm doing wrong when using ereg(). This is my regexp: ereg("^]$", "]"); and that does'n work, but this does: ereg("^$", "[");
9
by: Fiore Alessandro | last post by:
Hi all I've installed Apache 2.0.48 and PHP 4.3.4 on a workstation running Linux, RedHat 9.0. Let's consider the following piece of code: $patt = "{3}/{6}"; if...
3
by: Martin Lucas-Smith | last post by:
Is there some way of using ereg to detect when certain filename extensions are supplied and to return false if so, WITHOUT using the ! operator before ereg () ? I have an API that allows as an...
3
by: Aphrael | last post by:
Hello all, last time, ereg specialists did help me, so I ask again: base pattern: some texte her result: the number alone So ? any idea ? Aphrael ;-)
3
by: Rafal Zak | last post by:
I have a little problem with ereg (eregi) in PHP - in some cases it behaves differently than I expect and I don`t know if my expectations are strange or there is any "syntax subtlety" I have...
3
by: Dynamo | last post by:
Hi, Thanks to all who replied to original posting.As a direct result of now gaining a better understanding of regular expressions, instead of trying to use other peoples scripts I am now trying to...
3
by: news | last post by:
I'm trying to make sure a form has only one or two digits in either of two fields. I looked at php.net and http://www.regular-expressions.info/reference.html and this is what I put together, but...
18
by: yawnmoth | last post by:
Say I have the following script: <? $string = 'test'; if (eregi("^+$",$string)) { echo 'matches!'; } else {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.