<?php
function str_replace_once($from, $to, $str) {
if(!$newStr = strstr($str, $from)) {
return $str;
}
$iNewStrLength = strlen($newStr);
$iFirstPartlength = strlen($str) - $iNewStrLength;
return substr($str, 0, $iFirstPartlength).$to.substr($newStr, strlen($from), $iNewStrLength);
}
$s = 'Joe is here but jack is not here.';
echo str_replace_once('here', 'xxx', $s);
// will display
// Joe is xxx but jack is not here.
?>
|