473,657 Members | 2,419 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 1520
On Sat, 04 Dec 2004 18:19:51 GMT, "LRW" <de**@celticbea r.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.andyhsoftwa re.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.c om...
On Sat, 04 Dec 2004 18:19:51 GMT, "LRW" <de**@celticbea r.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
6911
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 file name. I can successfully extract the TITLE section, but no luck with the AUTHOR part. Any idea why?
5
2759
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 page for a text match, and return true or false. eregi returns false whatever the case, similar_text returns true whatever the case. Can someone sort me out - oops, HELP me out ?
2
4029
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")); Both resulting in 0 (zero) also tried and without result
25
6473
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 $email = $_POST // Use John Coggeshalls script to validate the email address if(!eregi("^+(\.+)*@+(\.+)*(\.{2,3})$", $email) { echo "The e-mail was not valid";
3
1726
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= "http://".$_SERVER.$_SERVER; // If the referring URL and the URL of this page don't match then // display a message and don't send the email.
6
3538
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", $str) && eregi("that", $str)
1
1722
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: alphanumerics, spaces, underscore, hyphen, period, apostrophe if(eregi("^+$", $value)) { return true; }
4
2455
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"> '; eregi('(=")(pics/)(+...)(")',$htmlsource,$imagesintext); ?>
4
1866
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 (only one comma at each time). I thought that the code for only containing letters would be: eregi("^()", $keywords);
0
8310
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8826
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.