如何从默认位置替换字符串


How to replace a string from a default position?

我有以下代码:

$text = "abc def abc jkl mno";
    $regex = '/'.trim('abc').'/ism';
    $search = array();
    if(preg_match_all($regex, $text, $tmp)) {
       if(isset($tmp[0])) {             
          for($i = 0;$i < count($tmp[0]); $i++) {
            $search = $tmp[0][1];
            $replace = 'ghi';
            $article = str_replace($search, $replace, $text);   
          }
       }
    }
        echo $article;

当我echo $article_text时,结果是:ghi def ghi jkl mno

但我想要的结果是abc def ghi jkl mno.我该如何解决这个问题?

试试这个: http://codepad.viper-7.com/dJZIJk

$text = "abc def abc jkl mno";
$search = 'abc';
$replace = 'ghi';
$article = substr($text, 0, strlen($search)) . str_replace($search, $replace, substr($text, strpos($text, 'abc') + strlen($search)));
echo $article;