Solved

Need to create function similar to ISNUMERIC()

Posted on 2007-11-16
9
428 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
  • 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
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…

757 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

19 Experts available now in Live!

Get 1:1 Help Now