dongyi1441 2016-09-30 21:04 采纳率: 100%
浏览 53
已采纳

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

What's wrong with this preg_match() usage? I want to check steam lobby link and if it's matching then write to database. If not, just echo the error. I am doing this through ajax. Is it better to do this with ajax or $_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();
    }
}
?>

My Problem: When I execute this code, it will give me false.

Thank you for help.

  • 写回答

1条回答 默认 最新

  • dseigqk7443 2016-09-30 21:20
    关注

    This works:

    $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.";
    }
    

    I changed /joinlobby to //joinlobby, and remove the / at the end. I also removed the unnecessary () around everything.

    I suspect you also shouldn't have (...)+ around steam?: and //joinlobby/730/. They'll cause repeated uses of those prefixes to be accepted as correct, e.g. steam:steam:...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持