Solved

Need to create function similar to ISNUMERIC()

Posted on 2007-11-16
9
463 Views
Last Modified: 2008-02-01
Need to make a function that is similar to ISNUMERIC() but does not see numerical signs ( + - * / ) as valid.

Is there such a function in php?

ex - variable $m comes back true if == "12" & comes back false if == "/2"
0
Comment
Question by:drews77
[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
  • 5
  • 4
9 Comments
 
LVL 20

Expert Comment

by:steelseth12
ID: 20300245
function my_numeric($str) {

      if(preg_match("/^\d+(\.\d+)*(e\d+)*$/",$str)) {
      
            return true;
            
      }else{
      
            return false;
      }
}
0
 

Author Comment

by:drews77
ID: 20300391
thanks for the function.   Can a function be placed in an IF statement?

ex-

if (my_numeric("$m") && my_numeric("$d")) {
   echo "This will print";  }
0
 
LVL 20

Accepted Solution

by:
steelseth12 earned 125 total points
ID: 20300424
Yes it can.
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

by:drews77
ID: 20301201
Fatal error: Cannot redeclare my_numeric() (previously declared in C:\Program Files\xampp\htdocs\convert_date.php:6) in C:\Program Files\xampp\htdocs\convert_date.php on line 6

I'm pulling dates that are incorrectly formated from a database and converting them to yyyy-mm-dd format... however I get an error msg after it try to convert the second date (see above).  Not sure why I cant you a function twice....   Here's what the query looks like:
 
 include("convert_date.php");  // convert date

  $table = "rolist";
  include("db_fnc.php");  // connecting to MYSQL database

  $sql = @mysql_query("SELECT ro, indate, outdate, open FROM $table_name
  WHERE open = 'T'
 ", $db);

  if ($myrow = mysql_fetch_row($sql)) {
 
  do {

  convert_date($myrow['1']);

  } while ($myrow = mysql_fetch_array($sql));

} else { echo "Sorry, no records were found!";      }
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20301224
You can not declare a function more than 1 time ...
I cant tell from your code where you include it twice but you can avoid this kind of error by declaring your function like this

if(!function_exists(my_numeric)) {

      function my_numeric($str) {
      
            if(preg_match("/^\d+(\.\d+)*(e\d+)*$/",$str)) {
            
                  return true;
                  
            }else{
            
                  return false;
            }
      }      
}
0
 

Author Comment

by:drews77
ID: 20301357
Each time it pulls a row it runs the date through convert_date() function.  Within the convert_date() is the my_numeric() ....odd that it doesnt give an error with the convert_date function.
0
 

Author Comment

by:drews77
ID: 20301407
Looks like I declared a function within a function....  that's it...  
Here is part of the function in a function...

 <?

function convert_date($indate) {


function my_numeric($str) {

      if(preg_match("/^\d+(\.\d+)*(e\d+)*$/",$str)) {

            return true;

      }else{

            return false;
      }
}

function my_numeric_false($str) {

      if(preg_match("/^\d+(\.\d+)*(e\d+)*$/",$str)) {

            return false;

      }else{

            return true;
      }
}
......................
}

thanks
0
 

Author Comment

by:drews77
ID: 20301583
Can  I refer to a function in a function?  Having trouble...

Seems like it not recognizing the function my_numeric().  
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20303499
Yes function have the global scope . they can be called inside other function even if they were defined outside.
e.g

a();

function a() {

      print b();

}


function b() {

      return "hello world";
}
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…
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.

730 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