在PHP中,可以使用以下方法进行字符串匹配:
$string = "Hello, world!";
$substring = "world";
if(strpos($string, $substring) !== false){
echo "Match found!";
} else {
echo "Match not found!";
}
$string = "Hello, world!";
$substring = "world";
if(strstr($string, $substring)){
echo "Match found!";
} else {
echo "Match not found!";
}
$string = "Hello, world!";
$pattern = "/world/";
if(preg_match($pattern, $string)){
echo "Match found!";
} else {
echo "Match not found!";
}
$string = "Hello, world! Hello, universe!";
$pattern = "/Hello/";
if(preg_match_all($pattern, $string, $matches)){
echo "Matches found: " . count($matches[0]);
} else {
echo "No matches found!";
}
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: php判断是否为空的方法是什么