Solved

how to check data for double quotes using ereg fn?

Posted on 2002-05-01
5
298 Views
Last Modified: 2012-08-14
hai,
$piece[2]='"-25.53"';
if(ereg("^\-{0,1}[0-9]{0,}\.{0,1}[0-9]{0,}$",trim($piece[2]))){
//other codes
}// this line is to check for the data -25.53.
but I want to check for double quotes also.
so how do I do? anyother alternative code is also welcome.

Thanks
0
Comment
Question by:baska
  • 3
  • 2
5 Comments
 
LVL 7

Accepted Solution

by:
axis_img earned 50 total points
ID: 6985102
Hello...

first thing, are the double quotes optional, or are they mandatory. I have provided four different examples for your needs. Two are using ereg(), and two are using the much faster preg_match(). Try them all and let me know how they work for you.

// Value
$piece[2]='"25.53"';

// EREG - QUOTES OPTIONAL EXAMPLE
if(ereg("^\"?-?[0-9]{0,}\.?[0-9]{0,}\"?$", trim($piece[2]))) {
     //other codes
}

// EREG - QUOTES MANDATORY EXAMPLE
if(ereg("^\"-?[0-9]{0,}\.?[0-9]{0,}\"$", trim($piece[2]))) {
     //other codes
}

// PREG - QUOTES OPTIONAL EXAMPLE
if(preg_match("/^\"?-?[0-9]*\.?[0-9]*\"?$/U", trim($piece[2]))) {
     // other codes
}

// PREG - QUOTES MANDATORY EXAMPLE
if(preg_match("/^\"-?[0-9]*\.?[0-9]*\"$/U", trim($piece[2]))) {
     // other codes
}


I would suggest using preg_match(), as it is normally a much faster counterpart to ereg().

Also, you were using limiters such as {0,1} and {0,}... Those can be substituted with:

{0,1} == ?
{0,} == *
{1,} == +

Those characters can be used in lieu of the {x,y} method.

Hope that helps...
Barry
0
 
LVL 7

Expert Comment

by:axis_img
ID: 6985104
0
 
LVL 8

Expert Comment

by:us111
ID: 6985515
<?
$piece[2]='"-25.53"';
if(ereg("^\"\-{0,1}[0-9]{0,}\.{0,1}[0-9]{0,}\"$",trim($piece[2]))){
    print "ok";
}
?>



0
 
LVL 7

Expert Comment

by:axis_img
ID: 6986629
I'm curious, us111... Why did you you just post a repeat of my code? Other than the fact that I optimized {0,1} with "?", and removed the unnecessary escaping \ from in front of the -, it is the exact same code. That seems a bit odd to me that you would do that.

Anyway, just another note on using preg_match(). You really should look into it, because ereg() is much much slower.

This is the better option.

if(preg_match("/^\"-?[0-9]*\.?[0-9]*\"$/U", trim($piece[2]))) {
    // other codes
}

If you are not sure about how some of it works, let me know.

Regards,
Barry
0
 
LVL 8

Expert Comment

by:us111
ID: 6993336
hmm well I don't know , sorry  axis_img I was tired :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now