$d = array(
  '国内' => '中国',
  '中国' => '国内',
);
echo strtr('中国国内', $d);国内中国

解决方案 »

  1.   

    版主历害, 参考把网上的加多行代码, 立马行了echo strtr_words('那个阿谁回结果成果百孔千疮-哀鸿遍野中人');
    function strtr_words($str)
    {
        $words=array();
        $content = file_get_contents('_web/word.txt');//词库
        $content = str_replace( "\r", "",$content); //去掉换行符(以便兼容Linux主机)
        $content = preg_split('/\n/', $content, -1, PREG_SPLIT_NO_EMPTY);//\n分割字符
            foreach($content as $k=>$v)
            {
                if($k!=0)
                {
                    $str_data = explode('-',$v);//关键词分割符
                    $words+=array("$str_data[0]"=>"$str_data[1]");
                    $words+=array("$str_data[1]"=>"$str_data[0]");//多一行反向同义也替换
                }
            }
        return strtr($str,$words);//返回结果
    }