Solved

how to check data for double quotes using ereg fn?

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

770 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