元问答栏目视频美女
  1. 编程问答
  2. 答案列表

php 查找字符是否存在

回答列表
在 php 中,你可以使用多种方法来查找字符是否存在。以下是其中几种常用的方法:
使用 strpos() 函数:
$string = "hello,world!";
$char = "o";
if (strpos($string,$char)!== false) {
echo "字符 '$char'存在于字符串中。";
} else {
echo "字符 '$char'不存在于字符串中。";
}
使用 str_contains() 函数(需要 php 8.0 或更高版本):
$string = "hello,world!";
$char = "o";
if (str_contains($string,$char)) {
echo "字符 '$char'存在于字符串中。";
} else {
echo "字符 '$char'不存在于字符串中。";
}
使用 preg_match() 函数:
$string = "hello,world!";
$char = "o";
if (preg_match("/$char/",$string)) {
echo "字符 '$char'存在于字符串中。";
} else {
echo "字符 '$char'不存在于字符串中。";
}
这些方法都可以用来查找字符是否存在于字符串中。你可以根据自己的需求选择适合的方法。
猜你喜欢
相关推荐