Scott B
Scott B

Reputation: 40167

How to determine if $match is in $string?

$string = "Test String for the test";
$match = "test string";

How to determine if $match is in $string?

Upvotes: 1

Views: 231

Answers (3)

Reese Moore
Reese Moore

Reputation: 11640

use strpos to check if it is in the string. API Link

For case insensitive, use stripos. API Link

Upvotes: 1

Christian Mann
Christian Mann

Reputation: 8125

Use stristr($haystack, $needle): http://php.net/manual/en/function.stristr.php

$boolean = stristr($string, $match);

Upvotes: 0

Gumbo
Gumbo

Reputation: 655319

You can use stripos to find the position of $match in $string with a case-insensitive search:

$pos = stripos($string, $match);

Note to compare this with a type-safe comparison operator like === or !==. Because if $match is at the begin of $string like in this case, stripos returns 0 and (boolean) 0 === false.

Upvotes: 2

Related Questions