温馨提示×

php stripos如何查找子字符串

PHP
小樊
83
2024-11-16 19:46:47
栏目: 编程语言
PHP开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

stripos 是 PHP 中的一个字符串函数,用于查找一个字符串中首次出现另一个字符串的位置

<?php
$haystack = "Hello, welcome to the world of PHP!";
$needle = "PHP";

$position = stripos($haystack, $needle);

if ($position !== false) {
    echo "子字符串 '{$needle}' 在主字符串 '{$haystack}' 中的位置是: " . $position;
} else {
    echo "子字符串 '{$needle}' 未在主字符串 '{$haystack}' 中找到.";
}
?>

在这个示例中,我们查找子字符串 “PHP” 在主字符串 “Hello, welcome to the world of PHP!” 中的位置。stripos 函数返回子字符串首次出现的位置(从 0 开始计数)。如果子字符串不存在于主字符串中,则返回 false

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:php stripos如何处理特殊编码

0