How can the position of a string that occurs multiple times in a text be determined using PHP functions like strpos and strrpos?

When a string occurs multiple times in a text, you can use the PHP functions strpos and strrpos to determine the position of the first and last occurrence of the string, respectively. The strpos function returns the position of the first occurrence of the string in the text, while the strrpos function returns the position of the last occurrence. By using these functions in combination, you can find the positions of all occurrences of the string in the text.

$text = "This is a sample text with multiple occurrences of the word 'sample'.";
$keyword = "sample";

$first_occurrence = strpos($text, $keyword);
$last_occurrence = strrpos($text, $keyword);

echo "First occurrence of '$keyword' is at position: $first_occurrence<br>";
echo "Last occurrence of '$keyword' is at position: $last_occurrence";