How can the issue of removing a specific word from a string in PHP be resolved without affecting other occurrences of the word in the text?

When removing a specific word from a string in PHP, we need to be careful not to unintentionally remove other occurrences of the word in the text. One way to resolve this issue is to use a combination of string manipulation functions like `str_replace` and `preg_replace` to target only the specific word we want to remove while leaving other instances untouched.

<?php
$string = "This is a sample sentence with the word 'example' appearing multiple times. Let's remove the word 'example' without affecting other occurrences.";

$wordToRemove = 'example';
$string = preg_replace('/\b' . $wordToRemove . '\b/', '', $string);

echo $string;
?>