How to Check if String Starts with Specific Word in PHP?
Hi Dev,
This post is focused on how to check if string starts with specific word in php?. I explained simply about how do i check if a string contains a specific word in php?. I would like to share with you do i check if a string contains a specific word?. This article goes in detailed on check if a string starts with a specific character in php?.
There are example to php check if string starts with specific word in php. in this example, we will use to startwith() function to check if string starts with specific word in . so let's the following example with output.
Example 1:
index.php
<?php
// Function to check string starting
// with given substring
function startsWith ($string, $startString)
{
$len = strlen($startString);
return (substr($string, 0, $len) === $startString);
}
// Main function
if(startsWith("bbcde","c")){
echo "True";
}
else {
echo "False";
}
?>
Output:
False
Example 2:
index.php
<?php
// Function to check string starting
// with given substring
function startsWith ($string, $startString)
{
$len = strlen($startString);
return (substr($string, 0, $len) === $startString);
}
// Main function
if(startsWith("abcde","a")){
echo "True";
}
else {
echo "False";
}
?>
Output:
True
I hope it could help you...
- How To Use Php Conditional Statement Example
- How to Check if String Contains Regex in PHP?
- PHP Array_column() Function Example
- PHP Generate List of Random Numbers Between 0 and 100 Example
- How to User Login with Facebook in PHP?
- How to Explode Every Character into Array Using PHP Example?
- PHP Form Validation Tutorial
- PHP Convert XML to JSON Example