Solved

how to check data for double quotes using ereg fn?

Posted on 2002-05-01
5
309 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

752 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