473,503 Members | 2,142 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

simple eregi not working (PEBCAK)

LRW
I'm trying to make sure a search text field has only numbers, letters, or
spaces.
I started with this:

$pattern = "^([ 0-9a-z])";
if (eregi($pattern, $search_txt)) {

And that works so long as the 1st character isn't a number or letter,
otherwise it allows the whole string.
I did some newsgroup looking, and everything I've found has been for very
complex checks like e-mail addresses.
The simplest example I could find is:

$pattern = "[a-z0-9_\-]+";
if (eregi($pattern, $search_txt)) {

Which evidently also checks against only 1 letter searches, which is also
useful. But it still allows searches that contain both valid and invalid
characters. It only refuses patters that are only invalid.

This is something that's so rediculously simple...but is still beyond me. =/
Any suggestions?

Thanks,
Liam
Jul 17 '05 #1
3 1511
On Sat, 04 Dec 2004 18:19:51 GMT, "LRW" <de**@celticbear.com> wrote:
I'm trying to make sure a search text field has only numbers, letters, or
spaces.
I started with this:

$pattern = "^([ 0-9a-z])";
if (eregi($pattern, $search_txt)) {

And that works so long as the 1st character isn't a number or letter,
otherwise it allows the whole string.


You have:

^([ 0-9a-z])

... which only looks at the first character. You probably want:

^[ 0-9a-z]+$

(matches if string consists entirely of good characters)

Or

[^ 0-9a-z]

(matches if string contains at least one bad character)

^ in a pattern matches start of line.
^ in a character class inverts the character class.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #2
LRW
"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:9u********************************@4ax.com...
On Sat, 04 Dec 2004 18:19:51 GMT, "LRW" <de**@celticbear.com> wrote:
I'm trying to make sure a search text field has only numbers, letters, or
spaces.
I started with this:

$pattern = "^([ 0-9a-z])";
if (eregi($pattern, $search_txt)) {

And that works so long as the 1st character isn't a number or letter,
otherwise it allows the whole string.
You have:

^([ 0-9a-z])

... which only looks at the first character. You probably want:

^[ 0-9a-z]+$


Yep that did it. Sheesh, how simple! I can't believe I didn't get the
parentheses and dollarsign usage from what I looked at.
^ in a pattern matches start of line.
^ in a character class inverts the character class.


More good info to know. Thanks!
Liam
Jul 17 '05 #3
.oO(LRW)
Yep that did it. Sheesh, how simple! I can't believe I didn't get the
parentheses and dollarsign usage from what I looked at.


And now you should consider using preg_* instead of the old ereg*
functions. They are much more powerful and often faster.

Micha
Jul 17 '05 #4

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

Similar topics

5
6906
by: Jane Doe | last post by:
Hi I took a quick look in the archives, but didn't find an answer to this one. I'd like to display a list of HTML files in a directory, showing the author's name between brackets after the...
5
2750
by: george | last post by:
(driving me nuts) Hi there. I wonder if anyone can help? I'm including a page from Google in search.php, passing some parameters. So far so good. Then I'm asking to look through that Google...
2
4021
by: Frank | last post by:
I'm having trouble detecting whitespaces in strings. Set up this test: echo "<br>example 1:".intval(eregi("^\s","teststring")); echo "<br>example 2:".intval(eregi("^\s","test string")); ...
25
6438
by: Dynamo | last post by:
Hi The following script was taken from John Coggeshall's (PHP consultant) in his article on Zends site at http://www.zend.com/zend/spotlight/ev12apr.php // Get the email address to validate...
3
1716
by: Dynamo | last post by:
Hi I have used the following script within a simple form email to prevent the form being used from an external url. <?php $referer = $_SERVER; // Get the URL of this page $myurl=...
6
3532
by: deko | last post by:
To match "this" or "that" in string $str, I believe I would use this: eregi("(this|that)", $str) but what if I want to match "this" AND "that"? I suppose I could use this: eregi("this",...
1
1717
by: news | last post by:
God, I have read every comment in php.net eregi and Google searched, and I have tried so many different attempts...this is the closest I've gotten to verify a variable contains only:...
4
2447
by: Nel | last post by:
Hi all, I am struggling with understanding a small eregi problem in php4. My code: <?PHP $htmlsource = '<img src="pics/hotdog.gif"> text text <img src="pics/silly%20sausage.gif"> ';...
4
1852
by: ollie.mitch | last post by:
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...
0
7205
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
7093
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
7349
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...
1
7008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5594
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5022
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3168
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
746
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.