Error with pre_match [closed]
I am still kinda noobie with pre_match. I am trying to get the pre_match used for a zip code this is the error I get when I go to the page.
Parse error: syntax error, unexpected '/', expecting T_VARIABLE or '$' in /home/valerie2/public_html/el开发者_如何学JAVAinkswap/snorris/findstore.php on line
And this is my code:
<?php
$zip;
if (pre_match("/^[0-9]{5{$/", $zip)) {
echo " The Zip code must be a 5-digit number.";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Find Store</title>
<style type="text/css" title="text/css" media="all">
.error {
color: #F30;
}
h3 {
color: #00F;
}
body {
background-color: # FFCF73;
}
</style>
</head>
<body>
<form method="post" action="distance.php">
<p>Zip: <input type="text" name="zip" /><input type="submit" name="submit" value="find!"/></p>
</form>
</body>
</html>
I know I did something wrong and I have been going through it and everything. I am just wondering if someone can help me understand what I am doing wrong.
<?php
$zip;
if (pre_match("/^[0-9]{5{$/", $zip)) {
echo " The Zip code must be a 5-digit number.";
}
?>
Pretty much everything about this is wrong. Try this instead:
<?php
if( preg_match("/^[0-9]{5}$/", $zip))
$error = "The Zip code must be a 5-digit number.";
?>
<!DOCTYPE .........
....
....
<?php echo $error ?>
Things corrected:
- removed extra space at the beginning of the script
- preg_match spelled correctly
- regex had
{
in place of}
- error message printed in the document, not somewhere before the doctype
In:
pre_match("/^[0-9]{5{$/", $zip)) {
Use single quotes like:
pre_match('/^[0-9]{5{$/', $zip)) {
I did not check the rest of your code.
See: http://us.php.net/manual/en/language.types.string.php#language.types.string.parsing for more about what a $ means inside double quotes.
精彩评论