I need to remove the underscore and anything that comes after it. If there is no underscore the string should be left as it is. Is is possible?
$str1 = 'green_apples';
$str1 = substr($str1, 0, strpos($str1, '_'));
echo $str1; // green
Works fine until the string contains no underscore:
$str2 = 'yellow';
$str2 = substr($str2, 0, strpos($str2, '_'));
echo $str2; // should be 'yellow' but nothing is printed