Posted By

jatkins on 01/16/11


Tagged

String replacement manipulation strreplace


Versions (?)

str_replace_first


 / Published in: PHP
 

Released into the public domain.

  1. function str_replace_first($needle, $replacement, $haystack) {
  2. $needle_start = strpos($haystack, $needle);
  3. $needle_end = $needle_start + strlen($needle);
  4. if($needle_start!==false) {
  5. $to_replace = substr($haystack, 0, $needle_end);
  6. return str_replace($needle, $replacement, $to_replace) . substr($haystack, $needle_end);
  7. }
  8. else
  9. return $haystack;
  10. }

Report this snippet  

You need to login to post a comment.