如何检查固定字符串是否包含任何其他字符?

How to check if a fixed string contain any additional character?

此代码检查 "have" 是否存在于字符串中,假设字符串 总是 以 "I have found" 开头我需要的是一个检查是否存在的函数该字符串包含 "I have found" 加上其他内容。示例:我找到了 500。其中 500 可以是任何东西,而且是未知的。

 $a = 'I have found';
 if (strpos($a, 'have') !== false) {
 echo 'true';
 }

如果您想知道找到了什么:

function get_found($str){
    if(strpos($str, "I have found")===false)
        return "nothing";
    $found = trim(substr($str, strlen("I have found")));
    if($found == "")
        return "nothing";
    return $found;
}

echo get_found("I have found a friend"); //outputs "a friend"
echo get_found("I have found"); //outputs "nothing"

您可以使用 preg_match(),就像在这段代码中:

$a = 'I have found'; //fixed string
$str = 'I have found 500';
if (preg_match('/^'.$a.'(.+?)$/', $str, $m)){
 echo 'The string contains additional: '.$m[1];
}
else echo 'String fixed';