[PHP] Points to note when checking whether a specified string/pattern is included

table of contents
Introduction
This is Kusakabe from the web team.
I often forget what the return values of strpos() and preg_match() are, so I've put together a list of some minor points to note as a reminder. This
article focuses only on whether the specified string/pattern is included, and only explains some of the functions, so please refer to the manual for details on each function.
Overall
When comparing values, use === and !== instead of == and != unless you have a reason not to.
We recommend looking at a type comparison table , especially if you are surprised that 0 == FALSE is TRUE
table of contents
- Checks whether a string contains a specified string
- Checks whether a string contains a specified pattern
- Extracting elements that match a pattern from an array
Checks whether a string contains a specified string
strpos() [ Manual ]
int strpos(string $heystack, mixed $needle)
argument
heystack: The string to search for.
needle: The string to search for. You can specify a type other than a string, but... (see below)
Return Value
Returns the position where needle first appears in heystack (starting with 0).
If needle is not included in heystack, it returns FALSE.
strpos($heystack, $needle) !== FALSE
You can check for existence by looking at the value of
Note 1
If needle appears at the top of heystack, the return value is 0
strpos($heystack, $needle) != FALSE
will return FALSE if the string appears at the beginning. Be sure to compare the types as well
Note 2
In the manual
If needle is not a string, convert it to an integer and treat it as the character corresponding to that number
However, this
does not mean
that "if needle is not a string, it is cast to an integer and then cast back to a string." (Though casting to an integer is correct.)
If needle is not a string, it is treated as the character that corresponds to the integer-casted needle as a decimal ASCII code.
In other words, the following two are not equivalent:
strpos('2100', 100) strpos('2100', (string)(int)100) // needle becomes the string 100
The following three are equivalent:
strpos('2100', 100) strpos('2100', chr((int)100)) strpos('2100', 'd')
To avoid unintended results, it is better to specify a string for needle unless there are special circumstances.
For more information on chr(), see
PHP: chr - Manual
Note 3
No error occurs if you put an array into needle, but this does not mean that you can specify multiple needles; it just means that the array is cast to an integer
#As of PHP 7.0, casting an array to an integer will return 0 for an empty array and 1 for a non-empty array
strstr() [ Manual ]
This function finds the position where needle first appears and returns the string from there to the end of the string.
If needle is not found, it returns FALSE, so it can be used to check existence, but it is not recommended to use this function only for that purpose.
Note:
If you just want to check whether a needle is in a particular haystack, use strpos() instead, which is faster and uses less memory.
Checks whether a string contains a specified pattern
preg_match() [ Manual ]
int preg_match(string $pattern, string $subject)
Return Value
If there is a string that matches the pattern in the subject, it returns 1; if not, it returns 0; if an error occurs during the matching process, it returns FALSE
preg_match($pattern, $subject) === 1
Just check if it is TRUE
Points to note
If you need to distinguish between no match and an error, be aware that 0 == FALSE is TRUE
Extracting elements that match a pattern from an array
Here, the array
$input = ['first', 'second', 'third', 'fourth', 'fifth'];
Let's consider an example where we want to extract elements that end with 'th' from a string.
For this, it's easy to use preg_grep().
preg_grep() [ Manual ]
array preg_grep(string $pattern, array $input)
Return Value
of an array that extracts elements from input that match the pattern
:
preg_grep('/th$/', $input) // ['fourth', 'fifth']
this is
array_filter($input, function($s) { return preg_match('/th$/', $s) === 1; })
is equivalent to
Points to note
If you specify the constant PREG_GREP_INVERT as the third argument of preg_grep(), that do not match will be extracted. Elements that will also be extracted if the matching process results in an error. In other words, the following two are equivalent.
preg_grep('/th$/', $input, PREG_GREP_INVERT) array_filter($input, function($s) { return preg_match('/th$/', $s) !== 1; })
#There is no constant defined to explicitly specify "extract elements that match the pattern", so if you want to extract matching elements, omit the third argument or specify 0
2