title: Php remove underscore if there is one and anything that comes after it
body: <p>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?   </p>
  
  <pre><code>$str1 = 'green_apples';
  $str1 = substr($str1, 0, strpos($str1, '_'));
  echo $str1; // green
  </code></pre>
  
  <p>Works fine until the string contains no underscore:</p>
  
  <pre><code>$str2 = 'yellow';
  $str2 = substr($str2, 0, strpos($str2, '_'));
  echo $str2; // should be 'yellow' but nothing is printed
  </code></pre>