wppaste
WordPress

stripos( $haystack, $needle )

Source
wp-includes/class-pop3.php:662

Finds where a substring first occurs inside another string while ignoring letter case, exactly like PHP's own stripos(). WordPress only defines its own version of this function inside a function_exists() guard, so on every supported PHP build your code actually calls PHP's native stripos(), offset argument and all, not the two-parameter version shown in core. The core copy is built by combining strpos() with stristr(), which shapes what it returns when nothing matches. Reach for strpos() instead when case should matter, or mb_stripos() when the haystack may contain multibyte characters.

Compatibility

WordPress
core
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$haystack
$needle

Code examples

Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.

Search post titles case-insensitively for a keyword

Loop over the five baseline posts and flag any title that contains a word regardless of how it's capitalized.

$posts = get_posts( array( 'numberposts' => 5, 'orderby' => 'ID', 'order' => 'ASC' ) );
foreach ( $posts as $post ) {
    $found = stripos( $post->post_title, 'HELLO' ) !== false;
    printf( '%s: %s<br>', esc_html( $post->post_title ), $found ? 'matches keyword' : 'no match' );
}

Post 1's title "Hello world!" matches even though the search term is written in all caps.

Detect a mail transport by name regardless of case

Check a sendmail-style path for the string "qmail" the same way core's mail classes sniff for it.

$sendmail_path = '/usr/sbin/Qmail-inject';

if ( stripos( $sendmail_path, 'qmail' ) !== false ) {
    echo esc_html( 'Mail transport looks like qmail: ' . $sendmail_path );
} else {
    echo esc_html( 'Mail transport is not qmail-based.' );
}

Common problems and fixes · 3

Why does my if(stripos(...)) check fail even though the match is right at the start of the string?

A match at the very beginning of the haystack returns the integer 0, and 0 is falsy in PHP, so a loose truthiness check treats a real match as if nothing was found.

Why can't I pass a third offset argument to stripos()?

The version defined in WordPress core only declares $haystack and $needle, unlike PHP's native stripos() which accepts an optional offset. This copy is wrapped in a function_exists() guard, so:

Why would this function return 0 instead of false when the search term genuinely isn't in the string?

The core implementation calls stristr() first and feeds its result into strpos(). When stristr() finds no match it returns false, and strpos() coerces that false into an empty string, which strpos() reports as matching at position 0 rather than returning false.

Alternatives and related functions

strpos
When the comparison must be case-sensitive, since strpos() will not match "Hello" against a search for "hello".
stristr
When you need the matched substring itself rather than its numeric position.
mb_stripos
When the haystack or needle contains multibyte characters and byte-based position counting would be wrong.
preg_match
When the search is more than a plain substring, such as matching a pattern with the case-insensitive 'i' modifier.

Performance profile

How much work a call to stripos() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
8

Executed per call on PHP 8.5. The body compiles to 8.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
41

41 places in core call this, so the cost is paid more often than your own code shows.

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost stripos() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always8stristr()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev880
8.5880
8.48803 fewer instructions than PHP 8.3
8.311110
8.211110
8.111110
7.411110

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Used by · 41

Show all 41

Source code

    function stripos($haystack, $needle){        return strpos($haystack, stristr( $haystack, $needle ));    }

Changelog

Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/class-pop3.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.