substr_compare

substr_compare(string $haystack, string $needle, int $offset, ?int $length = null, bool $case_insensitive = false): int

Compares a window of haystack starting at the given offset against needle: equivalent to strcmp(substr(haystack, offset, length), needle) — a -1/0/+1 tri-state; length caps the window, case_insensitive ignores case; the single-function composition of the existing strcmp/substr entries.

C C23

C23
#include <stdio.h>
#include <string.h>
#include <strings.h> /* strcasecmp: POSIX */

/* pointer arithmetic is the offset: strncmp(s + offset, ...) is the whole trick */
int main(void) {
    const char *s = "abcde";
    printf("%d\n", strncmp(s + 1, "bcde", 4) == 0); /* full window */
    printf("%d\n", strncmp(s + 1, "bc", 2) == 0);   /* length caps the window */
    printf("%d\n", strcasecmp(s + 1, "bcde") == 0); /* the case_insensitive flag */
    printf("%d\n", strncmp(s + 1, "abc", 4) > 0);   /* tri-state: b > a */
    return 0;
}
Output
1
1
1
1

C++ C++26

C++26
#include <iostream>
#include <string>

// string::compare(pos, len, other) IS substr_compare: pos = offset,
// len = length, and the return is the same -1/0/+1 tri-state
int main() {
    std::string s = "abcde";
    std::cout << (s.compare(1, 4, "bcde") == 0) << "\n"; // full window
    std::cout << (s.compare(1, 2, "bc") == 0) << "\n";   // length caps the window
    std::cout << (s.compare(1, 4, "BCDE") != 0) << "\n"; // case-sensitive by default
    std::cout << (s.compare(1, 4, "abc") > 0) << "\n";   // tri-state: b > a
    return 0;
}
Output
1
1
1
1

C# v11.0.100

v11.0.100
using System;

// string.Compare overloads carry the offsets: the closest single-call analog
var s = "abcde";
Console.WriteLine(string.Compare(s, 1, "bcde", 0, 4, StringComparison.Ordinal) == 0); // True - full window
Console.WriteLine(s.Substring(1, 2) == "bc"); // True - window by hand
Console.WriteLine(string.Compare(s, 1, "BCDE", 0, 4, StringComparison.OrdinalIgnoreCase) == 0); // True - ignore-case flag
Console.WriteLine(string.Compare(s, 1, "abc", 0, 3, StringComparison.Ordinal) > 0); // True - tri-state
Output
True
True
True
True

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"strings"
)

// slicing + compare: no substr_compare in std - offset windows are s[offset:]
func main() {
	s := "abcde"
	fmt.Println(s[1:] == "bcde")                  // true - full window compare
	fmt.Println(strings.HasPrefix(s[1:], "bc"))   // true - length-capped window
	fmt.Println(strings.EqualFold(s[1:], "BCDE")) // true - case-insensitive
	fmt.Println(s[1:] > "abc")                   // true - tri-state lexicographic
}
Output
true
true
true
true

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        String s = "abcde";
        // regionMatches: the direct analog - offset on both sides plus a length
        System.out.println(s.regionMatches(1, "bcde", 0, 4));       // true - full window
        System.out.println(s.regionMatches(1, "bc", 0, 2));         // true - length caps the window
        System.out.println(s.regionMatches(true, 1, "BCDE", 0, 4)); // true - ignoreCase flag
        System.out.println(s.regionMatches(1, "abc", 0, 3));        // false - "bcde" vs "abc"
        // compareTo gives the tri-state, but only from the string head - hence substring first
        System.out.println(Integer.signum(s.substring(1).compareTo("abc"))); // 1
    }
}
Output
true
true
true
false
1

JavaScript v26.13.1

v26.13.1
// slice + compare: no substr_compare in std - offset windows are s.slice()
const s = "abcde";
console.log(s.slice(1) === "bcde"); // true - full window
console.log(s.slice(1, 3) === "bc"); // true - length-capped window
console.log(s.slice(1).toLowerCase() === "bcde"); // true - case-insensitive window
console.log(s.slice(1) > "abc"); // true - tri-state lexicographic
Output
true
true
true
true

PHP v8.6.0

v8.6.0
// substr_compare(): strcmp over a window of haystack starting at offset -
// the single-function version of the existing substr + strcmp pairing
var_dump(substr_compare("abcde", "bcde", 1)); // window "bcde" == needle -> 0
var_dump(substr_compare("abcde", "bc", 1, 2)); // length caps the window at 2 -> 0
var_dump(substr_compare("abcde", "BCDE", 1, null, true)); // case_insensitive -> 0
var_dump(substr_compare("abcde", "abc", 1)); // "bcde" vs "abc" -> 1 (b > a)
var_dump(substr_compare("abcde", "abd", 0, 3)); // "abc" vs "abd" -> -1 (c < d)
Output
int(0)
int(0)
int(0)
int(1)
int(-1)

Python v3.15.2

v3.15.2
s = "abcde"

# slice-and-compare is the handwritten form; str.startswith carries the offset natively
print(s[1:] == "bcde")            # True - offset-1 window, full compare
print(s.startswith("bcde", 1))    # True - std API with the offset built in
print(s[1:3] == "bc")             # True - the length-2 window
print(s[1:].casefold() == "BCDE".casefold())  # True - case-insensitive window
print(s[1:] > "abc")              # True - lexicographic tri-state
Output
True
True
True
True
True

Rust v1.99.1

v1.99.1
// byte-slice + compare: offsets are byte indices (ASCII here - non-ASCII
// windows need char_indices); eq_ignore_ascii_case plays the flag role
fn main() {
    let s = "abcde";
    println!("{}", &s[1..] == "bcde");                   // true - full window
    println!("{}", s[1..].starts_with("bc"));            // true - length-capped window
    println!("{}", s[1..].eq_ignore_ascii_case("BCDE")); // true - case-insensitive
    println!("{}", &s[1..] > "abc");                     // true - tri-state lexicographic
}
Output
true
true
true
true

TypeScript v26.13.1

v26.13.1
// slice + compare: no substr_compare in std - offset windows are s.slice()
const s: string = "abcde";
console.log(s.slice(1) === "bcde"); // true - full window
console.log(s.slice(1, 3) === "bc"); // true - length-capped window
console.log(s.slice(1).toLowerCase() === "bcde"); // true - case-insensitive window
console.log(s.slice(1) > "abc"); // true - tri-state lexicographic
Output
true
true
true
true

Language differences

Composition vs single function: the existing strcmp_strncmp entry owns whole-string comparison and substr owns windowing — this entry fuses both steps and returns the same -1/0/+1 tri-state; the deterministic assertions: substr_compare("abcde","bcde",1)=0, with length=2 the "bc" window is 0, case-insensitive is 0, "bcde" vs "abc" is +1, "abc" vs "abd" is -1 — fixed inputs, identical everywhere.

PHP: substr_compare($h, $n, $offset, ?$length, $case_insensitive); a null $length compares to the end; since 8.0 an out-of-range $offset throws ValueError (it used to return false); case-insensitivity is $case_insensitive=true.

Python: no single call — s[offset:] == needle is the handwritten slice form; str.startswith(prefix, start) is the std API that carries the offset (but answers a boolean, no tri-state); case-insensitivity uses casefold() (safer than lower for cross-locale comparison).

Go: s[offset:] slice plus ==; the length-capped window is strings.HasPrefix; case-insensitivity is strings.EqualFold (Unicode-aware, wider than an ASCII-only flag).

Java: regionMatches(toffset, other, ooffset, len) is the only std API carrying offsets on both sides — closest to the PHP shape, but it returns a boolean, not a tri-state; the regionMatches(ignoreCase, ...) overload maps to $case_insensitive; the tri-state comes from compareTo plus Integer.signum.

C#: string.Compare(sA, offsetA, sB, offsetB, length, StringComparison) is the single-call analog with offsets on both sides; StringComparison.OrdinalIgnoreCase maps to $case_insensitive.

JavaScript/TypeScript: s.slice(offset[, end]) plus comparison operators; the lexicographic > and < compare UTF-16 code units (identical to PHP's byte comparison in the ASCII range).

C: strncmp(s + offset, needle, len) — pointer arithmetic is the offset; strcasecmp (POSIX, strings.h) plays the case-insensitive flag; the example compiles with -D_GNU_SOURCE and both Linux/macOS provide the symbol.

C++: string::compare(pos, len, other) maps parameter-by-parameter to the PHP shape and returns the same tri-state — the closest-shaped member of the ten languages.

Rust: &s[offset..] is a byte slice — with non-ASCII text a byte offset can land inside a multi-byte sequence (panic), so locate boundaries with char_indices; eq_ignore_ascii_case covers ASCII only, full-Unicode case folding needs a third-party crate.