What are common pitfalls when trying to remove the rest of a string from array elements after a specific word in PHP?

When trying to remove the rest of a string from array elements after a specific word in PHP, a common pitfall is not using the correct string manipulation functions like strstr or substr. To solve this issue, you can iterate through the array elements and use strstr to find the specific word, then use substr to extract the desired portion of the string.

<?php
$array = ["Hello World", "Goodbye World", "PHP is awesome"];

$specificWord = "World";

foreach ($array as $key => $value) {
    $pos = strstr($value, $specificWord);
    if ($pos) {
        $array[$key] = substr($value, 0, strpos($value, $specificWord) + strlen($specificWord));
    }
}

print_r($array);
?>