How to check match the first & last word from a variable in php?

In this post, we use php preg_match to match the first & last word in a variable with a given first & last specific words.

1. Code:

<?php
$first_word = 't'; 
$last_word = 'ne';
$str = 'this function can be done';

if(preg_match('/^' . $first_word . '(.*)' . $last_word .'$/' , $str))
{
     echo 'true';
}
?>

2. Example:

Input:

$first_word = 't'; 
$last_word = 'ne';
$str = 'this function can be done';

Output:

Output: true