Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to check data for double quotes using ereg fn?

Posted on 2002-05-01
5
Medium Priority
?
323 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 200 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 Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

963 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