/    Sign up×
Community /Pin to ProfileBookmark

ereg function not working

Hello, everyone. I am having a problem with an ereg function
in my all in one php form.
The script has a text input for someone to enter their percent bodyfat (without the % sign) and I am trying to create an error statement so that if someone enters either a % or letters
a warning will come saying:
“You have entered either a % sign or letters instead of numbers. Please check and submit again.”
Without this error statement, the form works fine, so it is definitely the ereg function. Please don’t suggest that I use
the subs() function, because It only works for the first letter. In theory the subs () function should work for the assigned string length, but let me tell you: IT DOESN’T.
well anyhow the script looks like this:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html>
<head>
<title></title>
</head>
<body>
<table width=”100%” height=”100%” cellpadding=”0″ cellspacing=”0″ border=”0″ align=”center”>
<tr>
<td width=”100%” height=”100%” valign=”middle”>
<?
$form_block = “
<form method = “post” action = “$PHP_SELF”>
<table border=0 cellpadding=5 cellspacing=0 >
<tr>
<td><b>Name</b></td>
<td colspan=6><input type=text size=35 maxlength=25 name=name /></td>
</tr>
<tr>
<td ><b>Bodyweight (lbs)</b></td>
<td colspan=6><input type=text size=8 maxlength=3 name=weight /></td>
</tr>
<tr>
<td><b>Sex</b></td>
<td colspan=6>
<select name=gender>
<option value=male >Male</option>
<option value=female selected>Female</option>
</select>
</td>
</tr>
<tr>
<td><b>% Bodyfat</b><br /><font size=”1″>(without the % sign)</font></td>
<td><input type=”text” size=”5″ maxlength=”2″ name=”fat” /></td>
</tr>
<tr><td colspan=7 height=50>&nbsp;</td></tr>
<input type=submit name=submit value=Calculate size=20></td>
</tr>
</table>
</form>
“;

// Check that all input fields are filled.

if (($name == “”) || ($weight == “”) || ($fat == “”))
{
echo “
<table border=”0″ cellspacing=”0″ cellpadding=”0″ width=”100%” align=”center”>
<tr><td height=”20″ align=”center”><b>One or more fields were left blank. Please check and submit the form again.</b></td></tr>
<tr><td height=”20″ align=”center”><hr width=”100%” align=”center” /></td></tr>
<tr><td height=”20″ align=”center”>&nbsp;</td></tr>

</table>”;
echo “$form_block”;

}

// Check there is no percent sign or letters in bodyfat.

else if (ereg(“([0-9])” , $fat)) {
echo “
<table border=”0″ cellspacing=”0″ cellpadding=”0″ width=”100%” align=”center”>
<tr><td height=”20″ align=”center”><b>You have entered a letter(s) or a percent % sign in place of a number. Please check and submit the form again.</b></td></tr>
<tr><td height=”20″ align=”center”><hr width=”100%” align=”center” /></td></tr>
<tr><td height=”20″ align=”center”>&nbsp;</td></tr>

</table>”;
echo “$form_block”;

}

else {

if ($gender == “female”){
$k1 = .9;
$k2 = 24;
{
if ($fat <= 18) {$fat = 1.0;}
else if (($fat >18) && ($fat <= 28)) {$fat = .95;}
else if (($fat >28) && ($fat <= 38)) {$fat = .90;}
else {$fat = .85;}
}
$mass= $weight/(2.2050);
$bmr= $mass * $k1* $k2 * $fat;
$roundbmr= round($bmr);
}

else{
$k1 = 1;
$k2 = 24;
{
if ($fat <= 14) {$fat = 1.0;}
else if (($fat >14) && ($fat <= 20)) {$fat = .95;}
else if (($fat >20) && ($fat <= 28)) {$fat = .90;}
else {$fat = .85;}
}
$mass = $weight/(2.2050);
$bmr = $mass * $k1* $k2 * $fat;
$roundbmr = round($bmr);
}

echo “
<table border=”0″ cellpadding=”5″ cellspacing=”0″ width=”100%” height=”100%” align=”center” class=”result”>
<tr>
<td align=”left” valign=”middle”>
$name, your body needs a minimum intake of $roundbmr calories/day to function properly.
This is your BMR (Basic or Basal Metabolic Rate) and its calculations were based on your gender and your
bodyweight of $weight lbs.
</td>
</tr>
</table>”;
}

?>
</td></tr></table>
</body>
</html>

<!–In this version of percent, I removed the hidden input precedent to the submit button
to process the form. By simplifying the form in this way I am isolating the ereg function and
testing it to see how it interacts with other functions and control statements.
As I figure this out, I will make the form more complicated again.–>
<!–when I tested the form, I got the ereg error statement regardless if I entered a single or
double digit, therefore the ereg range is not wrong, but how it’s format or location in the form–>
<!–In trial3 I added an exit(); to the ereg. Upon testing it I saw that the mistake remained–>
<!–In trial4 I got rid of the ereg statements to see if the form even works without it.–>
<!–Okay, the form works without ereg function, so that is the problem–>
<!–In trial5, I redid the parentheses the way Jonathan at crosspoints.org had it in the ereg
function.There is an error and it is not working. I am taking the form to a php forum.–>

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@JonaJan 11.2004 — [font=arial]Doesn't ereg() need a delimiter? Like / or #?[/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@pyroJan 11.2004 — [i]Originally posted by Jona [/i]

[B][font=arial]Doesn't ereg() need a delimiter? Like / or #?[/font][/B][/QUOTE]
Nope. ?

Christopher, you might be interested in the [URL=http://us2.php.net/is_numeric]is_numeric()[/URL] function, as there is really no need to invoke a regex engine for what you are trying to do.

Also, you said: "In theory the subs () function should work for the assigned string length, but let me tell you: IT DOESN'T."

There is no subs() function, did you mean substr()? If so, what makes you say it should work?
Copy linkTweet thisAlerts:
@christopherauthorJan 11.2004 — Okay, I finally found the problem with the ereg function it was missing a ^ and now it is working. I am glad you told me about the is_numeric function. I had no idea that it existed, but now that I know I will put it to use.

yes I did spell substr() function wrong. Sorry. I got the idea for its use from Bill Pellowe at

http://www.eltcalendar.com on his mileage script. He uses the substr() function to check that the first character written is not a letter. But then I found ereg can do the samething. thanks for all the help.
Copy linkTweet thisAlerts:
@pyroJan 11.2004 — [i]Originally posted by christopher [/i]

[B]He uses the substr() function to check that the first character written is not a letter. But then I found ereg can do the samething.[/B][/QUOTE]
Yep, for something like that, it'd be easier to use regex than substr(). One thing to keep in mind - PCRE are much more powerful than POSIX regex, so if you need to do any serious work, that's probably what you'll want to use.
×

Success!

Help @christopher spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...