RegEx导致无限循环/错误-请求的实体太大


RegEx results in infinite loop/error - Requested entity too large

下面的代码应该检查是否在字符串中找到了#@符号。正则表达式应该找到每个@#,并将找到的每个实例放入messages表中(如果它是@符号),或者如果它是一个#符号,它应该将该实例插入hashtags表中,或者如果标签已经在表中,则更新现有记录。

目前,脚本本身运行良好,但当通过AJAX使用带有javascript的代码时,控制台的响应是说请求的实体(这个脚本)太大(或类似的东西)。我认为它会陷入一个无休止的循环,但到目前为止,我还没有找到更好的方法。那么,有什么更好的编码方式呢?

if (preg_match_all("/[@]+[A-Za-z0-9-_]+/i", $post, $matches)) {
        for ($i = 0;$i <= $matches;$i++) {
            $match = str_replace("@", "", $matches[$i]);
            foreach($match as $key=>$mVal) {
                $uMSQL = "INSERT INTO `messages` (`to`, `from`, `message`, `sent`) VALUES (:to, :from, '<p>tagged you in a post</p>', NOW())";
                $uMQ = $con->prepare($uMSQL);
                $uMQ->bindParam(':from', $author, PDO::PARAM_STR);
                $uMQ->bindParam(':to', $mVal, PDO::PARAM_STR);
                $uMQ->execute();
            }
        }
    }
    if (preg_match_all("/[#]+[A-Za-z0-9-_]+/i", $post, $hashtags)) {
        for ($h = 0; $h <= $hashtags; $h++) {
            $htMatched = $hashtags[$h];
            foreach($htMatched as $key=>$htVal) {
                $htCheck = "SELECT COUNT(hashtag) FROM `hashtags` WHERE `hashtag` = '$htVal'";
                $htQ = $con->query($htCheck);
                $htExistence = $htQ->fetchColumn();
                if ($htExistence >= 1) {
                    $addTU = "UPDATE `hashtags` SET `used` = used+1 WHERE `hashtag` = '$htVal'";
                    $updateHT = $con->exec($addTU);
                } else {
                    $htMSQL = "INSERT INTO `hashtags` (`hashtag`) VALUES (:hashtag)";
                    $htMQ = $con->prepare($htMSQL);
                    $htMQ->bindParam(':hashtag', $htVal, PDO::PARAM_STR);
                    $htMQ->execute();
                }
            }
        }
    }

AJAX

 function sendData() {
    var hr = new XMLHttpRequest();
    var url = "http://localhost/NextIt/ajax/sendPost.php";
    var txtField = window.frames['richTextField'].document.body.innerHTML;
    var access = document.getElementById('postTo').selectedIndex;
    var acc = document.getElementById('postTo').options;
    var accss = acc[access].text;
    var vars = "post="+txtField+"&access="+accss;
    hr.open("POST", url, true);
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    hr.onreadystatechange = function() {
    if (hr.readyState === 4 && hr.status === 200) {
        var return_data = hr.responseText;
        $('status').innerHTML = return_data;
        window.frames['richTextField'].document.body.innerHTML = '';
    }
}
    hr.send(vars);
    $('status').innerHTML = "Posting...";
}

php_ini中的post_max_size为8M。它通过AJAX Post将数据发送到PHP脚本(如上)。我得到的唯一"错误"是在控制台中(例如"请求的实体太大")除此之外,控制台中没有返回任何内容。但是,它确实阻止了location.reload()行的执行。我真的没有什么"问题"可以定义。脚本是有效的,但有些东西导致脚本变得太大,我认为这是因为PHP脚本被困在一个无限循环中。

if (preg_match_all("/(?<=@)['w-]+/i", $post, $matches)) {
    foreach($matches[0] as $mVal) {
        $uMSQL = "INSERT INTO `messages` (`to`, `from`, `message`, `sent`) VALUES (:to, :from, '<p>tagged you in a post</p>', NOW())";
        $uMQ = $con->prepare($uMSQL);
        $uMQ->bindParam(':from', $author, PDO::PARAM_STR);
        $uMQ->bindParam(':to', $mVal, PDO::PARAM_STR);
        $uMQ->execute();
    }
}
if (preg_match_all("/#+['w-]+/", $post, $hashtags)) {
    foreach($hashtags[0] as $htVal) {
        //Assuming that the `hashtag` column is a unique key on this table
        //WHICH IT SHOULD BE...
        $htMSQL = "INSERT INTO `hashtags` (`hashtag`) VALUES (:hashtag) ON DUPLICATE KEY UPDATE SET `used` = `used` + 1";
        $htMQ = $con->prepare($htMSQL);
        $htMQ->bindParam(':hashtag', $htVal, PDO::PARAM_STR);
        $htMQ->execute();
    }
}

无论如何,这应该会让你更接近。我可能建议减少对数据库的调用(例如,在这种情况下,无论帖子有多大,你都可以只调用两个),以确保事务安全并提高一点速度。