duanliang9288 2018-07-06 16:22 采纳率: 100%
浏览 32

PHP检查编号字符串是否遵循模式

I am comparing inputs of different version numbers looking like so:

testname-v01.03.001.01
testname-v02.01.001.03
...

I am doing a comparison to make sure that no inputs are being maliciously entered into my textbook to harm my sql tables.

What I am doing is something like this:

<?php
    function startsWith($needle, $haystack){
        return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== false;
    }
    $reqmethod = $_SERVER["REQUEST_METHOD"];
    $textInput = "";
    if( $reqmethod == "GET") {
        $textInput = $_GET["my_input"];
    }
    $stringComparison = "v02.01.001.01";
    if ( $textInput != ""){
        $valid_input = startsWith("testname", $textInput); #See if text starts with version
        #if not check if its a partial match
        if (!$valid_input){
            if(preg_match('/^[A-Z][0-9]+.[0-9].[0-9].[0-9]', $textInput)){
            $textInput= "version-" + $textInput;
        } else {
            $textInputReadOut = "BAD VALUE";
            $textInput= "";
        }
    }
?>

To get the preg_match to equal say v01 would I just go about that by doing something like this: [A-Z][0-9][0-9]? I have tried that but the variable returns a BAD VALUE instead

Referenced : Checking a string against a pattern

  • 写回答

2条回答 默认 最新

  • doulu4534 2018-07-06 16:28
    关注

    Your regular expression is matching strings that start with (^) v1.2.3.4, but your $textInput starts with testname (or presumably other package names).

    You also have it coded that the regular expression only checks if the input is invalid. Your text input is always valid, because it always starts with testname, meaning you always land in your else wherein the output is BAD VALUE. Any time your $textInput starts with testname, the output is BAD VALUE, as far as your code is currently concerned.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大