How to Check if String Starts with Specific Word in PHP?

Nov 07, 2022

.

Admin

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...

#PHP