I have a string in $str
variable.
How can I verify if it starts with some word?
Example:
$str = "//somesite.com/somefolder/somefile.php";
When I wrote the following script returns yes
if[strpos[$str, "//"] == '0'] echo "yes";
BUT it returns yes even when I wrote
if[strpos[$str, "other word here"] == '0'] echo "yes";
I think strpos
returns zero if it can't find substring too [or a value that evaluates to zero].
So, what can I do if I want to verify if word is in the start of string? Maybe I must use ===
in this case?
[PHP 8]
str_starts_with — Checks if a string starts with a given substring
Description
str_starts_with[string $haystack
, string $needle
]: bool
Parameters
haystack
The string to search in.
needle
The substring to search for in the haystack
.
Return Values
Returns true
if haystack
begins with needle
, false
otherwise.
Examples
Example #1 Using the empty string
''
The above example will output:
All strings start with the empty string
Example #2 Showing case-sensitivity