str_contains
判断一个字符串中是否包含一个给定的子字符串
<h2>适用PHP版本</h2>
<p>PHP 8.0及以上版本。</p>
<h2>函数说明</h2>
<p>str_contains 函数用于检查字符串中是否包含某个子字符串。如果子字符串存在,返回 true,否则返回 false。</p>
<h2>函数语法</h2>
<p>bool str_contains(string $haystack, string $needle)</p>
<h2>参数</h2>
<ul>
<li><strong>$haystack</strong> (string): 需要搜索的主字符串。</li>
<li><strong>$needle</strong> (string): 要查找的子字符串。</li>
</ul>
<h2>返回值</h2>
<p>如果主字符串包含子字符串,则返回 true;如果不包含,则返回 false。</p>
<h2>示例</h2>
<p>以下是一个示例,演示了如何使用 str_contains 函数来检查字符串是否包含指定的子字符串。</p>
<h3>示例代码</h3>
<pre>
$haystack = "Hello, world!";
$needle = "world";
if (str_contains($haystack, $needle)) {
echo "字符串中包含子字符串!";
} else {
echo "字符串中不包含子字符串!";
}
<h3>示例代码的说明</h3>
<p>在上述代码中,我们定义了一个主字符串 $haystack 和一个子字符串 $needle。使用 str_contains 函数来检查 $haystack 中是否包含 $needle。如果包含,输出 "字符串中包含子字符串!";否则,输出 "字符串中不包含子字符串!"。</p>