1
2<?php
3$mystring = 'abc';
4$findme = 'a';
5$pos = strpos($mystring, $findme);
6
7// Note our use of ===. Simply == would not work as expected
8// because the position of 'a' was the 0th (first) character.
9if ($pos === false) {
10 echo "The string '$findme' was not found in the string '$mystring'";
11} else {
12 echo "The string '$findme' was found in the string '$mystring'";
13 echo " and exists at position $pos";
14}
15?>
16
17
1//str_contains ( string $haystack , string $needle ) : bool
2
3if (str_contains('Foo Bar Baz', 'Foo')) {
4 echo 'Found';
5}
1You could use regular expressions as its better for word matching compared to
2strpos, as mentioned by other users. A strpos check for are will also return
3true for strings such as: fare, care, stare, etc. These unintended matches can
4simply be avoided in regular expression by using word boundaries.
5
6A simple match for are could look something like this:
7
8$a = 'How are you?';
9
10if (preg_match('/\bare\b/', $a)) {
11 echo 'true';
12}