<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-comment">// This part is unrelated to the article content and can be initialization or other setup</span></span><span>
</span><span><span class="hljs-comment">// For example, setting the timezone</span></span><span>
</span><span><span class="hljs-title function_ invoke__">date_default_timezone_set</span></span><span>(</span><span><span class="hljs-string">'Asia/Shanghai'</span></span><span>);
</span><span><span class="hljs-meta">?></span></span><span>
<p><hr></p>
<p></span><?php<br>
/**</p>
<ul>
<li>
<p>How to use the second parameter of the stristr function?</p>
</li>
<li>
<p>Detailed explanation of string vs. array usage</p>
</li>
<li></li>
<li>
<p>In PHP, the stristr function searches for a substring within a string and returns the portion of the string starting from the first occurrence of that substring.</p>
</li>
<li>
<p>Its basic usage is:</p>
</li>
<li>
<p>string stristr(string $haystack, string $needle, bool $before_needle = false)</p>
</li>
<li>
<p>Where:</p>
</li>
<li>
<ul>
<li>
<p>$haystack is the string to search in</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>$needle is the string to search for</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>$before_needle optional, if true, returns the portion before the needle</p>
</li>
</ul>
</li>
<li></li>
<li>
<p>Some developers wonder: Can the second parameter of stristr be an array? What’s the difference between passing a string and an array?</p>
</li>
<li></li>
<li>
<ol>
<li>
<p>Type restriction for stristr's second parameter</p>
</li>
</ol>
</li>
<li>
<p>The second parameter of stristr must be a string and cannot be an array. Passing an array will trigger a PHP error:</p>
</li>
<li>
<p>Warning: stristr() expects parameter 2 to be string, array given</p>
</li>
<li></li>
<li>
<p>Therefore, strictly speaking, stristr only accepts a string as its second parameter.</p>
</li>
<li></li>
<li>
<ol start="2">
<li>
<p>Why the confusion about arrays?</p>
</li>
</ol>
</li>
<li>
<p>In some cases, developers want to check if a string contains any of multiple keywords. stristr only handles one string at a time.</p>
</li>
<li>
<p>A common solution is to store multiple keywords in an array and loop through them, calling stristr for each keyword.</p>
</li>
<li></li>
<li>
<ol start="3">
<li>
<p>How to implement multi-keyword matching (simulating array input)</p>
</li>
</ol>
</li>
<li>
<p>Example code:<br>
*/</p>
</li>
</ul>
<p>$keywords = ['apple', 'banana', 'orange'];<br>
$text = "I like banana and orange juice.";</p>
<p>$found = false;<br>
foreach ($keywords as $word) {<br>
if (stristr($text, $word) !== false) {<br>
$found = $word;<br>
break;<br>
}<br>
}</p>
<p>if ($found !== false) {<br>
echo "Found keyword: $found\n";<br>
} else {<br>
echo "No keywords found.\n";<br>
}</p>
<p>/**</p>
<ul data-is-last-node="" data-is-only-node="">
<li>
<p>4. Summary</p>
</li>
<li>
<ul>
<li>
<p>The second parameter of stristr must be a string; arrays are not allowed directly.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>For multi-keyword matching, loop through an array and call stristr for each keyword.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>This allows flexible checking if a string contains any of multiple keywords.</p>
</li>
</ul>
</li>
<li></li>
<li>
<ol start="5">
<li>
<p>Related functions</p>
</li>
</ol>
</li>
<li>
<ul>
<li>
<p>stripos: Case-insensitive search for string position, second parameter is also a string.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>preg_match: Can also use regex for multi-keyword matching, e.g., pattern /(apple|banana|orange)/i.</p>
</li>
</ul>
</li>
<li></li>
<li data-is-last-node="">
<p data-is-last-node="">The above provides a detailed explanation and example of using stristr's second parameter and the differences between strings and arrays.<br>
*/<br>
?><br>
</span>