21xrx.com
2024-09-17 04:49:34 Tuesday
登录
文章检索 我的文章 写文章
使用PHP进行字符串包含操作
2023-06-15 15:55:39 深夜i     --     --
PHP 字符串包含 strpos() strstr() preg_match() stristr()

在PHP中,字符串包含可以使用函数strpos()或strstr()来实现。下面是两个示例:


// 使用strpos()函数

$string = "Hello, world!";

if (strpos($string, "world") !== false)

  echo "字符串包含 'world'!";

// 使用strstr()函数

$string = "Hello, world!";

if (strstr($string, "world"))

  echo "字符串包含 'world'!";

在上面的示例中,第一次使用了strpos()函数,它返回在原始字符串中第一次出现指定字符串的位置。如果没有找到指定字符串,则返回false。因此,如果返回值不是false,则说明字符串包含指定字符串。

第二个示例使用了strstr()函数。该函数返回原始字符串从指定字符串开始的子串,如果没有找到指定字符串,则返回false。因此,如果返回值不是false,则说明字符串包含指定字符串。

除了以上两个函数,还有其他的函数可以用来实现字符串包含操作,例如preg_match()(用于正则表达式匹配)和stristr()(用于忽略大小写的字符串包含)。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复
    相似文章