Content · Programming · PHP

PHP stristr() Function: Usage and Examples

Returns part of a string beginning at a case-insensitive match.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

PHP stristr() returns part of a string beginning at a case-insensitive match. The examples below show how the call behaves, where it is useful, and which limitations matter.

What Is stristr() in PHP?


Returns part of a string beginning at a case-insensitive match.

Its call structure is:

stristr($haystack, $needle, [$before_needle])

The arguments identify the values or resources passed to stristr(). Read the examples with attention to the type of each supplied argument and to whether the function modifies its input or returns a separate result.

Example 1: Basic Usage


This example introduces stristr with a direct call and a small, readable input.

<?php

echo stristr("Hello PHP World", "php");

Output:

PHP World

Returns part of a string beginning at a case-insensitive match. Returns false if absent; use stripos() when only the position is wanted.

Example 2: Another Behavior or Input


Compare this input with the previous example: changing the data or option can affect how stristr behaves.

<?php

echo stristr("Contact: [email protected]", "@", true);

Output:

Contact: SUPPORT

The changed input illustrates why output may differ with the chosen values or environment. Returns false if absent; use stripos() when only the position is wanted.

Practical Example: Using the Feature


The following example incorporates stristr into a workflow. Values such as paths, connection handles and configuration options must come from trusted application settings.

<?php

$header = "Content-Type: application/json"; $matchedPart = stristr($header, "content-type:"); var_dump($matchedPart !== false);

Output:

bool(true)

In a real application, check each operation result, validate values before use and clean up acquired resources. Returns false if absent; use stripos() when only the position is wanted.

Important Behavior and Edge Cases


  • Purpose: Returns part of a string beginning at a case-insensitive match.
  • Key limitation: Returns false if absent; use stripos() when only the position is wanted.
  • Failure handling: Check return values and handle invalid inputs and unavailable operating-system or extension features where relevant.

Comparison With Related Functions


Function or ApproachBest Fit
stristr()Returns part of a string beginning at a case-insensitive match.
strstr()Returns the substring from the first match, rather than a numeric position.

Common Beginner Mistakes


MistakeCorrect Approach
Returns false if absent; use stripos() when only the position is wanted.The correct approach depends on this behavior: Returns false if absent; use stripos() when only the position is wanted.
Ignoring errors or cleanup after the operation.Validate inputs and check each result; close files, restore settings or release resources when the operation requires it.

Key Takeaways


  • Returns part of a string beginning at a case-insensitive match.
  • Returns false if absent; use stripos() when only the position is wanted.
  • Use the examples as starting points, and adapt environmental paths, resources and permissions rather than assuming fixed results.

stristr is useful when its specific behavior fits the problem. The safest implementations combine the correct API with input validation, error checking and resource cleanup.

Share this resourceSend this page to someone who may find it useful.