开发者

Error with pre_match [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 10 years ago.

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:

  1. removed extra space at the beginning of the script
  2. preg_match spelled correctly
  3. regex had { in place of }
  4. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜