stristr() is a built-in PHP string function that finds the first occurrence of a substring within another string and returns the portion of the string starting from that point. stristr() is case-insensitive, meaning it disregards differences in letter casing.
<span><span><span class="hljs-title function_ invoke__">stristr</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$haystack</span></span><span>, </span><span><span class="hljs-keyword">mixed</span></span><span> </span><span><span class="hljs-variable">$needle</span></span><span>, </span><span><span class="hljs-keyword">bool</span></span><span> </span><span><span class="hljs-variable">$before_needle</span></span><span> = </span><span><span class="hljs-literal">false</span></span><span>): </span><span><span class="hljs-keyword">string</span></span><span>|</span><span><span class="hljs-literal">false</span></span><span>
</span></span>
$haystack: The string to be searched.
$needle: The substring to search for.
$before_needle: If set to true, returns the portion of haystack before needle. Defaults to returning the portion from needle onwards.
<span><span><span class="hljs-variable">$string</span></span><span> = </span><span><span class="hljs-string">"Hello World!"</span></span><span>;
</span><span><span class="hljs-variable">$result</span></span><span> = </span><span><span class="hljs-title function_ invoke__">stristr</span></span><span>(</span><span><span class="hljs-variable">$string</span></span><span>, </span><span><span class="hljs-string">"world"</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$result</span></span><span>; </span><span><span class="hljs-comment">// Outputs "World!"</span></span><span>
</span></span>
In this example, stristr() ignores the case of the letters, so it successfully matches "world" and returns "World!".
preg_match() is a PHP function used for regular expression matching. It allows pattern-based string matching using regular expressions. Unlike stristr(), preg_match() supports complex matching rules, including character classes, quantifiers, boundaries, and case-insensitive options.
<span><span><span class="hljs-title function_ invoke__">preg_match</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$pattern</span></span><span>, </span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$subject</span></span><span>, </span><span><span class="hljs-keyword">array</span></span><span> &</span><span><span class="hljs-variable">$matches</span></span><span> = </span><span><span class="hljs-literal">null</span></span><span>, </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$flags</span></span><span> = </span><span><span class="hljs-number">0</span></span><span>, </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$offset</span></span><span> = </span><span><span class="hljs-number">0</span></span><span>): </span><span><span class="hljs-keyword">int</span></span><span>|</span><span><span class="hljs-literal">false</span></span><span>
</span></span>
$pattern: Regular expression pattern.
$subject: The input string.
$matches: If a match is found, this contains the results.
$flags: Optional flags to modify matching behavior.
$offset: The position in the string to start searching from.
<span><span><span class="hljs-variable">$string</span></span><span> = </span><span><span class="hljs-string">"Hello World!"</span></span><span>;
</span><span><span class="hljs-keyword">if</span></span><span> (</span><span><span class="hljs-title function_ invoke__">preg_match</span></span><span>(</span><span><span class="hljs-string">"/world/i"</span></span><span>, </span><span><span class="hljs-variable">$string</span></span><span>)) {
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Match found"</span></span><span>;
} </span><span><span class="hljs-keyword">else</span></span><span> {
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"No match"</span></span><span>;
}
</span></span>
Here, preg_match() uses the regular expression /world/i to match "Hello World!". The i modifier makes the match case-insensitive, so it successfully matches "world" regardless of case.
Function Complexity:
stristr() is a straightforward string search function, ideal for simple matching. It takes just the target string and the substring to look for, offering simple case-insensitive matching.
preg_match() is far more powerful, allowing intricate pattern-based matching using regular expressions with support for grouping, character classes, quantifiers, and more.
Performance:
stristr(), being simpler, is generally faster and more efficient for basic substring searches.
preg_match() involves compiling and evaluating regular expressions, which can be slower, especially for large strings or complex patterns.
Return Values:
stristr() returns the portion of the string from the match to the end. If no match is found, it returns false.
preg_match() returns 1 if a match is found, 0 if not, and optionally fills the $matches array with detailed match information.
Regex Support:
stristr() does not support regular expressions, limiting it to basic matching tasks.
preg_match() fully supports regex syntax, enabling advanced pattern recognition, capturing groups, backreferences, and more.
Use stristr() when:
You need a simple case-insensitive substring search. It’s ideal when performance matters and the matching task is straightforward.
Use preg_match() when:
You need advanced pattern matching or validation. It’s suitable for tasks requiring complex string analysis, pattern extraction, or conditional matching logic.