Php 不能在循环内调用函数

Php 不能在循环内调用函数,php,x-cart,Php,X Cart,我制作了一个PHP脚本,它循环遍历来自ajax请求的POST数据,并向相应的收件人发送电子邮件。在大多数情况下,脚本都可以工作,但是我有两个函数用于验证和清理POST数据 问题是这两个函数没有被调用,事实上,当电子邮件实际发出时,这两个函数作为字符串进行计算。我尝试了几种不同的方法使其相应地发挥作用,包括通过引用传入变量,但都没有效果。这里有人能发现这个问题吗 <?php error_reporting(E_ALL); function validate

我制作了一个PHP脚本,它循环遍历来自ajax请求的POST数据,并向相应的收件人发送电子邮件。在大多数情况下,脚本都可以工作,但是我有两个函数用于验证和清理POST数据

问题是这两个函数没有被调用,事实上,当电子邮件实际发出时,这两个函数作为字符串进行计算。我尝试了几种不同的方法使其相应地发挥作用,包括通过引用传入变量,但都没有效果。这里有人能发现这个问题吗

        <?php

    error_reporting(E_ALL);

    function validate($value){

        $value = strip_tags($value);
        $value = htmlspecialchars($value);
        $value = stripcslashes($value);

        return $value;
    }

    function detectUrl($string){
        $pattern = "/https?\:\/\/[^\" ]+/i";
        $val = preg_match_all($pattern, $string);

        if($val > 0){
            $replace = preg_replace($pattern, '', $string);
            return $replace;
        } else {
            return $string;
        }
    }

    if(isset($_POST) === true){
        # Include our DB connection
        if (!empty($include_func)) {
            require_once $_module_dir . XC_DS . 'func.php';
        }

        // Current date
        $date = date("d/m/y");

        // Get Form content
        $content = $_POST;

        // Begin message body
        #
        # Prepare message body
        ##
        $message   = array();
        $message[] = '<!DOCTYPE html>';
        $message[] = '<html>';
        $message[] = '<head>
                      <title>Sunglasses.ie Prescription form</title>
                      <style>
                        #wrapper {
                          width: 960px;
                          margin: 0 auto;
                          padding: 0 20px;
                        }
                        h1 {
                          text-align: center;
                        }
                        h3 {
                          text-align: center;
                          background: #eee;
                          border-top: 1px solid #000;
                          border-bottom: 1px solid #000;
                          padding: 5px 0;
                        }
                        table {
                          width: 100%;
                          margin-bottom: 20px;
                        }
                        table thead th {
                          border-bottom: 2px solid #ddd;
                          text-align: left;
                          vertical-align: bottom;
                        }
                        table tbody td {
                          border-bottom: 1px solid #ddd;
                          vertical-align: top;
                        }
                        table tfoot td {
                          border-top: 2px solid #000;
                          vertical-align: bottom;
                        }
                      </style>
                      </head>';
        $message[] = '<body>';
        $message[] = '<div id="wrapper">';
        $message[] = '<img src="test.jpg" alt="Sunglasses.ie">';
        $message[] = "<h1>A new Prescription has been generated: {$date}</h1>";
        $message[] = '<table>';
        $message[] = '<tbody>';


    foreach($content as $key => $value){
        $message[] = '<tr>';
        $message[] = "<td style='padding:8px;font-weight:bold;text-transform:uppercase;'>{$key}</td>";
        if(empty($value) || $value === null){
            $message[] = "<td style='text-transform:uppercase;'>0</td>";
        } else {
            $message[] = "<td style='text-transform:uppercase;'>{$value}</td>";
        }
        $message[] = '</tr>';
    }



    $stop = 5*ceil(((count($content)-3)/2)/5); # get array length to nearest multiple of 5


    /** Single POST Array **/
    for($row=3; $row<$stop+3; $row+=5){ // count $_POST entries
        $message[] = '<tr>';
        for($col=0; $col<5; $col++){ // simple column count
            if($col==2) {
                $message[] = "<td>&nbsp;</td>";
            } else if($col==0){
                if(!empty($_POST[$row])){
                    $message[] = "<td style='padding:8px;font-weight:bold;text-transform:uppercase;'>".$_POST[$row]."</td>";
                } else {
                    $message[] = "<td style='padding:8px;font-weight:bold;text-transform:uppercase;'>&nbsp;</td>";
                }
            } else if($col==3){
                if(!empty($_POST[$row+3])){
                    $message[] = "<td style='padding:8px;font-weight:bold;text-transform:uppercase;'>".$_POST[$row+3]."</td>";
                } else {
                    $message[] = "<td style='padding:8px;font-weight:bold;text-transform:uppercase;'>&nbsp;</td>";
                }
            } else if($j==1){
                if (!empty($_POST[$row+1])){
                    $message[] = "<td style='text-transform:uppercase;'>".detectUrl(validate($_POST[$row+1]))."</td>";
                } else {
                    $message[] = "<td style='text-transform:uppercase;'>&nbsp;</td>";
                }
            } else if($j==4){
                if (!empty($_POST[$row+4])){
                    $message[] = "<td style='text-transform:uppercase;'>".detectUrl(validate($_POST[$row+3]))."</td>";
                } else {
                    $message[] = "<td style='text-transform:uppercase;'>&nbsp;</td>";
                }
            }
        }
        $message[] = '</tr>';
    }


        $message[] = '</tbody>';
        $message[] = '</table>';

        #
        # Prepare email headers
        ##
        $to        = 'me@me.ie';
        $subject   = 'A new Prescription form has been generated';
        $headers   = array();
        $headers[] = "To: {$to}";
        $headers[] = 'From: Prescriptions ';
        $headers[] = 'Reply-To: Prescriptions - ';
        $headers[] =  "Subject: {$subject}";
        $headers[] = 'X-Mailer: PHP/'.phpversion();
        $headers[] = 'MIME-Version: 1.0';
        $headers[] = 'Content-type: text/html; charset=iso-8859-1';

        if(!empty($content)){
            #
            # Print message on the screen
            ##
            print implode("\r\n", $message);


            #
            # Send email to intended recipients
            ##
            mail('', $subject, implode("\r\n", $message), implode("\r\n", $headers));
            echo 'Email Sent!';
            echo '<pre>';
            print_r($_POST);
            echo '</pre>';
        } else {
            echo 'Mail not sent';
        }

        } else {        

            echo '<br>';
            echo 'Nah';
    }

就这么简单。许多电子邮件客户端会去掉
,通常会让你在使用CSS时感到不舒服。你必须像1999年那样使用HTML,并以内联方式完成所有工作!谢谢,我只是做了一些基本的格式化。问题是函数没有被调用:/@CianGallagher我想你编辑了你的问题,删除了导致问题的部分。对吗?我再也看不到
detectUrl(validate($\u POST[$row+1]))
。抱歉,已修复。删除了错误的段落此处有一个问题:
$\u POST['$row+3']
。删除那些单引号。这在“else if($col==3)”部分中。