PHP检测字符串是否以某个字符串开头

2023-12-24 15:56:28 举报文章

要检测一个字符串是否以 "lxm_" 开头,你可以使用 PHP 中的字符串函数 strpos() 或正则表达式函数 preg_match()

使用 strpos() 函数:

//by www.qzphp.cn
$string = "lxm_example";  
if (strpos($string, "lxm_") === 0) {  
    echo "字符串以 'lxm_' 开头";  
} else {  
    echo "字符串不以 'lxm_' 开头";  
}

使用 preg_match() 函数:

//by www.qzphp.cn
$string = "lxm_example";  
if (preg_match("/^lxm_/", $string)) {  
    echo "字符串以 'lxm_' 开头";  
} else {  
    echo "字符串不以 'lxm_' 开头";  
}

在这两个示例中,如果字符串以 "lxm_" 开头,将输出 "字符串以 'lxm_' 开头",否则输出 "字符串不以 'lxm_' 开头"。

如果你认为本文可读性较差,内容错误,或者文章排版错乱,请点击举报文章按钮,我们会立即处理!