Php 如何使用preg_replace从远程网站将日语翻译成英语

Php 如何使用preg_replace从远程网站将日语翻译成英语,php,utf-8,character-encoding,preg-replace,str-replace,Php,Utf 8,Character Encoding,Preg Replace,Str Replace,我正在尝试编写一个PHP脚本,将日本烹饪食谱从日语翻译成英语。这是一个宠物项目,不一定要完美。我的策略是 使用文件获取网站内容 将一些日语替换为英语(主要是成分名称) 将结果写入HTML文件 我正在从命令行运行此PHP脚本: <?php // Get contents of Japanese cooking website $url = 'http://recipe.igamono.jp/?eid=1077379'; $context = stream_conte

我正在尝试编写一个PHP脚本,将日本烹饪食谱从日语翻译成英语。这是一个宠物项目,不一定要完美。我的策略是

  • 使用文件获取网站内容
  • 将一些日语替换为英语(主要是成分名称)
  • 将结果写入HTML文件
  • 我正在从命令行运行此PHP脚本:

    <?php
    
        // Get contents of Japanese cooking website
        $url = 'http://recipe.igamono.jp/?eid=1077379';
        $context = stream_context_create(array('http' => array('header' => 'Accept-Charset: UTF-8, *;q=0')));
        $html = file_get_contents($url, false, $context);
    
        // Replace stuff
        $html = preg_replace('/right/s', 'foobar', $html); // works
        $html = preg_replace('/の/s', 'shazam!!', $html); // doesn't work
    
        // Write output to a file
        file_put_contents('output.html', $html);
    
    ?>
    

    试试
    $html=preg\u replace('/の/su、“'shazam!!”、$html)(带有/u UTF-8修饰符)


    更新:

    当使用/u修饰符时,输入文本($html)必须是UTF-8有效的,否则将不替换任何内容。输入文本编码可通过
    mb\u check\u编码($string,'UTF-8')
    检查建议:

        $html = preg_replace('/[\x{306E}]/u', 'shazam!!', $html);
    
    更新: 事实证明,您正在加载的页面不是UTF-8,而是EUC-JP。做:

    <?php
    
    // Get contents of Japanese cooking website
    $url = 'http://recipe.igamono.jp/?eid=1077379';
    $context = stream_context_create(array('http' => array('header' => 'Accept-Charset: EUC-JP, *;q=0')));
    $html = file_get_contents($url, false, $context);
    
    // Replace stuff
    $html = mb_convert_encoding($html, "UTF-8", "EUC-JP");
    $html = preg_replace('/right/s', 'foobar', $html); // works
    $html = preg_replace('/の/s', 'shazam!!', $html); // doesn't work
    
    // Write output to a file
    file_put_contents('output.html', $html);
    ?>
    

    谢谢你的回复。mb_detect_编码($html,'UTF-8')为true,但“严格”版本mb_detect_编码($html,'UTF-8',true)为false。另外,如果我需要/u修饰符,我的第二个示例也应该失败,对吗?添加/u修改器时,输出为空。该死!如果使用/su,则输出文件为空。如果使用just/s,我会得到:警告:preg_replace():编译失败:\x{}或\o{}中的字符值在偏移量8处太大。也许这是个好主意,但也许我的$html不是UTF-8?解决了这个问题。。但是:我错误地编辑了Mvorisek的答案,而不是我自己的答案。现在需要“同行评审”。。。问题是日文页面不是UTF-8,而是EUC-JP,需要先转换为UTF-8,以便の 我也解决了这个问题。我会将我的解决方案发布为我问题的编辑,但如果它也有效,我会接受你的。就这样!很高兴事情解决了。。
    
    <?php
        // Get contents of Japanese cooking website
        $url = 'http://recipe.igamono.jp/?eid=1077379';
        $html = file_get_contents($url);
    
        // Convert HTML to UTF-8 from Japanese
        $html = mb_convert_encoding($html, "UTF-8", "EUC-JP");
    
        // Replace stuff
        $html = preg_replace('/right/s', 'foobar', $html);
        $html = preg_replace('/の/s', 'shazam!!', $html);
    
        // Convert HTML back to Japanese character encoding
        $html = mb_convert_encoding($html, "EUC-JP", "UTF-8");
    
        // Write HTML to a file
        file_put_contents('output.html', $html);
    ?>
    
        $html = preg_replace('/[\x{306E}]/u', 'shazam!!', $html);
    
    <?php
    
    // Get contents of Japanese cooking website
    $url = 'http://recipe.igamono.jp/?eid=1077379';
    $context = stream_context_create(array('http' => array('header' => 'Accept-Charset: EUC-JP, *;q=0')));
    $html = file_get_contents($url, false, $context);
    
    // Replace stuff
    $html = mb_convert_encoding($html, "UTF-8", "EUC-JP");
    $html = preg_replace('/right/s', 'foobar', $html); // works
    $html = preg_replace('/の/s', 'shazam!!', $html); // doesn't work
    
    // Write output to a file
    file_put_contents('output.html', $html);
    ?>