Javascript 正则表达式导致无限循环/错误-请求的实体太大
以下代码应检查是否在字符串中找到了Javascript 正则表达式导致无限循环/错误-请求的实体太大,javascript,php,ajax,regex,Javascript,Php,Ajax,Regex,以下代码应检查是否在字符串中找到了#或@符号。正则表达式应该找到每个@或#,并将找到的每个实例放入消息表中(如果它是@符号),或者,如果它是#符号,则应将实例插入hashtags表中,或者如果hashtag已在表中,则应更新现有记录 目前,脚本本身工作正常,但当通过AJAX将代码与javascript一起使用时,控制台的响应是请求的实体(此脚本)太大(或类似的大小)。我认为它陷入了一个无休止的循环,但到目前为止,我还没有找到一个更好的方法来做到这一点。那么,什么是更好的编码方式呢 if (pre
#
或@
符号。正则表达式应该找到每个@
或#
,并将找到的每个实例放入消息
表中(如果它是@
符号),或者,如果它是#
符号,则应将实例插入hashtags
表中,或者如果hashtag已在表中,则应更新现有记录
目前,脚本本身工作正常,但当通过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();
}
}
}
}
php_ini中的post_max_大小为8M。它通过AJAX Post将数据发送到PHP脚本(如上)。我得到的唯一“错误”是在控制台中(例如“请求的实体太大”),除此之外,控制台中不会返回任何内容。但是,它会停止执行location.reload()行。没有一个我能真正定义的“问题”。脚本可以工作,但有一些因素导致脚本变得太大,我假设这是因为PHP脚本陷入了无限循环。
if(preg\u match\u all()/(?为什么要准备语句并在每次循环执行时绑定变量?这是一个典型的例子,无法理解准备语句的真正目的:准备语句一次,然后多次执行。嗯,像$hThank youFrankieTheKneeMan
和Mike W
这样的行感谢您的帮助。过去我我从来没有使用过数组、正则表达式、循环等等,所以这是一次学习经验。使用正则表达式本身导致了异常草率的代码,所以我将尽我所能,希望能够更好地处理所有这些。
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();
}
}
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();
}
}