在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!";
}