[英]How to match only the first letter of the string
我只想匹配字符串的第一个字母,即“ bot”。 例如:如果用户键入"bot hi"
,它将运行一个函数;如果用户键入"hi bot there"
它将不起作用
if(preg_match('[bot ]', strtolower($message))) {
$msg = str_replace('bot ', '', $message);
//Some message response
}
即使我输入"hi bot there"
,以上代码也能正常工作
您应该在字符串的开头使用^表示
if ( preg_match("/^bot (.*)/i", $message) ) {
$msg = str_replace('bot ', '', $message);
//Some message response
}
您可以使用strpos()
进行检查:
$str = "bot hi";
if (0 === strpos($str, 'bot')) {
echo("str starts with bot");
else{
echo("str does not starts with bot");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.