Current Location: Home> Latest Articles> What's the Difference Between stristr and Regular Expression (preg_match) in PHP?

What's the Difference Between stristr and Regular Expression (preg_match) in PHP?

gitbox 2025-06-22

1. The stristr() Function

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.

Syntax:

<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.

Example:

<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!".

2. The preg_match() Function

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.

Syntax:

<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> &amp;</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.

Example:

<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.

3. Differences Between stristr() and preg_match()

  1. 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.

  2. 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.

  3. 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.

  4. 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.

4. Use Cases

  • 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.