strstr
strstr(string $haystack, string $needle, bool $before_needle = false): string|false
Finds the first occurrence of a substring and returns the substring itself: from the match to the end by default, or the part before the match with before_needle=true; returns false on a miss.
C C23
#include <stdio.h>
#include <string.h>
int main(void) {
const char *email = "name@example.com";
const char *p = strstr(email, "@"); // the C namesake: hit -> pointer, miss -> NULL
if (p != NULL) {
printf("%s\n", p); // strstr($email, '@')
printf("%ld\n", (long)(p - email)); // byte offset — the strpos() return value
printf("%.*s\n", (int)(p - email), email); // strstr($email, '@', true)
}
printf("%d\n", strstr(email, "#") == NULL);
return 0;
}
@example.com 4 name 1
C++ C++26
#include <iostream>
#include <string>
int main() {
std::string email = "name@example.com";
std::size_t pos = email.find('@'); // npos means a miss, like PHP's false
if (pos != std::string::npos) {
std::cout << email.substr(pos) << "\n"; // strstr($email, '@')
std::cout << email.substr(0, pos) << "\n"; // strstr($email, '@', true)
}
std::cout << (email.find('#') == std::string::npos) << "\n";
return 0;
}
@example.com name 1
C# v11.0.100
string email = "name@example.com";
int pos = email.IndexOf('@'); // -1 means a miss, like PHP's false
Console.WriteLine(email.Substring(pos)); // strstr($email, '@')
Console.WriteLine(email.Substring(0, pos)); // strstr($email, '@', true)
Console.WriteLine(email.IndexOf('#') == -1);
@example.com name True
Go v1.27.4
package main
import (
"fmt"
"strings"
)
func main() {
email := "name@example.com"
pos := strings.Index(email, "@") // -1 means a miss, like PHP's false
if pos >= 0 {
fmt.Println(email[pos:]) // strstr($email, '@')
fmt.Println(email[:pos]) // strstr($email, '@', true)
}
fmt.Println(strings.Index(email, "#") < 0)
}
@example.com name true
Java v27.0.1
public class Main {
public static void main(String[] args) {
String email = "name@example.com";
int pos = email.indexOf('@'); // -1 means a miss, like PHP's false
System.out.println(email.substring(pos)); // strstr($email, '@')
System.out.println(email.substring(0, pos)); // strstr($email, '@', true)
System.out.println(email.indexOf('#') == -1);
}
}
@example.com name true
JavaScript v26.13.1
const email = "name@example.com";
const pos = email.indexOf("@"); // -1 means a miss, like PHP's false
console.log(email.slice(pos)); // strstr($email, '@')
console.log(email.slice(0, pos)); // strstr($email, '@', true)
console.log(email.indexOf("#") === -1);
@example.com name true
PHP v8.6.0
$email = 'name@example.com';
echo strstr($email, '@'), "\n"; // from the match to the end
echo strstr($email, '@', true), "\n"; // before_needle: everything before the match
var_dump(strstr($email, 'zzz')); // miss: false (strpos would return the POSITION)
@example.com name bool(false)
Python v3.15.2
email = "name@example.com"
head, sep, tail = email.partition("@") # partition: the closest native match to strstr
print(sep + tail) # strstr($email, '@'): from the match to the end
print(head) # strstr($email, '@', true): before the match
print(email.find("#")) # miss: -1, like strpos returning false
@example.com name -1
Rust v1.99.1
fn main() {
let email = "name@example.com";
match email.find('@') {
// Option forces handling the miss — the PHP false equivalent
Some(i) => {
println!("{}", &email[i..]); // strstr($email, '@')
println!("{}", &email[..i]); // strstr($email, '@', true)
}
None => println!("miss"),
}
println!("{}", email.find('#').is_none());
}
@example.com name true
TypeScript v26.13.1
const email: string = "name@example.com";
const pos: number = email.indexOf("@"); // -1 means a miss, like PHP's false
console.log(email.slice(pos)); // strstr($email, '@')
console.log(email.slice(0, pos)); // strstr($email, '@', true)
console.log(email.indexOf("#") === -1);
@example.com name true
Language differences
PHP: strstr($h, $n) returns the substring from the match to the end; the third parameter before_needle=true returns the part before the match; on a miss it returns false — use str_contains() when you only need existence (manual Note).
PHP's twin design: strstr returns the substring while strpos returns the position (a dedicated entry covers the latter); since PHP 8.0 an empty needle is legal, and a miss must be tested with === false.
C: strstr() is the namesake ancestor: hit -> pointer, miss -> NULL, and the pointer difference is the byte offset (the example prints it — that is exactly what strpos returns).
C++: find() returns the position (npos on a miss) and substr() cuts — a two-step "position, then substring".
Go: strings.Index returns -1/byte offset; check -1 before slicing, or it panics.
Java/JS/TS: indexOf returns -1; substring/slice extracts.
Python: str.partition() returns (before, sep, after) in one call — the idiomatic closest match to strstr's two forms; find returns -1 on a miss.
Rust: find returns Option<usize> (byte offset); matching Some/None forces the miss branch.
C#: IndexOf + Substring, same semantics.
Performance note (manual user note): a hit copies the substring, so strstr costs more memory than strpos when you only need existence.