Solved

how to check data for double quotes using ereg fn?

Posted on 2002-05-01
5
301 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Echo images using file system 2 29
Php pie charts 3 26
WordPress syntax 2 24
Change text to radio button and calendar form 2 32
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

929 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