函数无法正确格式化数字

时间:2019-09-30 20:27:20

标签: php function

我制作了一个联系表,用于从观众那里收集信息,除了电话号码格式外,它都正常工作,输入的内容似乎都没有格式。我不知道怎么了。

例如,如果我在表单中输入123 555 7890,它将返回为“ 1235557890”,而不是(123)555-7890。

<?php
error_reporting(0);
$msg = "";

if (isset($_POST['submit'])) {
    $to = "my_email";
    $subject = "Contact Form";
    $name = stripcslashes($_POST['name']);
    $email = stripcslashes($_POST['email']);
    $tel = stripcslashes($_POST['tel']);
    $message = stripcslashes($_POST['message']);

    function formatTelNumaber($tel) {
        $tel = preg_replace('/[^0-9]/', '', $tel);

        if (strlen($tel) > 10) {
            $countryCode = substr($tel, 0, strlen($tel) - 10);
            $areaCode = substr($tel, -10, 3);
            $prefix = substr($tel, -7, 3);
            $lineNumber = substr($tel, -4, 4);

            $tel = '+' . $countryCode . '(' . $areaCode . ') ' . $prefix . '-' . $lineNumber;
            echo $tel;
        } elseif (strlen($tel == 10)) {
            $areaCode = substr($tel, 0, 3);
            $prefix = substr($tel, 3, 3);
            $lineNumber = substr($tel, 6, 4);

            $tel = '(' . $areaCode . ') ' . $prefix . '-' . $lineNumber;
            echo $tel;
        } elseif (stat($tel) == 7) {
            $prefix = substr($tel, 0, 3);
            $lineNumber = substr($tel, 3, 4);

            $tel = $prefix . '-' . $lineNumber;
            echo $tel;
        }

        return $tel;
    }

    $msgBody = 'Name: ' . $name . "<br>" . 'Email:' . $email . "<br>" . 'Phone Number: ' . formatTelNumaber($tel) . "<br>" . 'Message: ' . "<br>" . $message;
    $headers = 'From: ' . $email;

    if (mail($to, $subject, $msgBody, $headers)) {
        $msg = "Message Sent Successfully!";
    } else {
        $msg = "Message Not Sent! Please Try Again";
    }
}
?>

<!DOCTYPE html>
<html>

    <head>
        <meta charset="utf-8">
        <meta name="author" content="me">
        <meta name="description" content="">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

        <title>Contact Form</title>

        <link href="css/normalize.css" rel="stylesheet">
        <link href="css/style.css" rel="stylesheet">
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    </head>

    <body class="bg-info">
        <div class="container">
            <div class="row justify-content-center">
                <div class="col-lg-4 bg-light rounded mt-3">
                    <h4 class="text-center text-dark p-2">Contact Form</h4>
                    <form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" class="p-2">
                        <div class="form-group">
                            <label>Name</label><br>
                            <input type="text" name="name" class="form-control" placeholder="Full Name" required>
                        </div>
                        <div class="form-group">
                            <label>Email</label><br>
                            <input type="email" name="email" class="form-control" placeholder="email@email.com" required>
                        </div>
                        <div class="form-group">
                            <label>Phone Number</label><br>
                            <input type="tel" name="tel" class="form-control" placeholder="(123) 555-8888" required>
                        </div>
                        <div class="form-group">
                            <label>Message</label><br>
                            <textarea name="message" class="form-control" rows="4" placeholder="Enter your message..." required></textarea>
                        </div>
                        <div class="form-group">
                            <input type="submit" name="submit" value="Send" class="btn btn-danger btn-block">
                        </div>
                        <div class="form-group">
                            <h4 class="text-center text-success"><?= $msg; ?></h4>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </body>

</html>

1 个答案:

答案 0 :(得分:0)

此行是错误的:

       } elseif (strlen($tel == 10)) {

== 10应该在函数调用之外:

       } elseif (strlen($tel) == 10) {

        } elseif (stat($tel) == 7) {

应该呼叫strlen(),而不是stat()stat()要求$tel是文件名,它返回有关文件的信息数组。