PHP preg_match()蒸汽链接验证错误

时间:2016-09-30 21:04:27

标签: php ajax regex

preg_match()用法出了什么问题?我想查看steam lobby链接,如果匹配则写入数据库。如果没有,只需回显错误。我是通过ajax这样做的。使用ajax或$_SERVER["REQUEST_METHOD"] == "POST"进行此操作会更好吗?

<?php
require("../includes/config.php");
$lobby = "steam://joinlobby/730/109775243427128868/76561198254260308";
if (!preg_match("%^((steam?:)+(/joinlobby\/730\/)+([0-9]{17,25}\/.?)+([0-9]{17,25})/$)%i", $lobby)) {
    echo "Lobby link isn't formatted correctly.";
}
else {
    $rank = "Golden";
    $mic = "No";
    try {
       $stmt=$db->prepare("INSERT INTO created_lobby (lobby_link, current_rank, have_mic) VALUES (:lobby_link, '$rank', '$mic')");
       $stmt->execute(array(
       ':input_link' => $_POST['lobbyLink']
       ));
    }
    catch(PDOException $e) {
       echo "Error: " . $e->getMessage();
    }
}
?>

我的问题: 当我执行这段代码时,它会给我错误。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

这有效:

$lobby = "steam://joinlobby/730/109775243427128868/76561198254260308";
if (!preg_match("%^(steam?:)+(//joinlobby/730/)+([0-9]{17,25}/.?)+([0-9]{17,25}$)%i", $lobby)) {
    echo "Lobby link isn't formatted correctly.";
}

我将/joinlobby更改为//joinlobby,并删除了最后的/。我还删除了所有内容中不必要的()

我怀疑你也不应该在(...)+steam?:附近//joinlobby/730/。它们会导致这些前缀的重复使用被接受为正确的,例如, steam:steam:...