如何使用 preg_replace 从远程网站将日语翻译成英语
How to use preg_replace to translate Japanese to English from a remote website
我正在尝试编写一个 PHP 脚本来将日本烹饪食谱从日语翻译成英语。这是宠物项目,不一定是完美的。我的战术是..
- 使用file_get_contents
获取网站内容
- 用英文替换一些日语(主要是成分名称)
- 将结果写入 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);
?>
我正在使用 Sublime Text 编辑文件 (translate.php),并且我已确保使用以下方式保存文件:文件/使用编码保存/UTF-8
当我运行脚本时,除了の替换外,一切正常。没有替换の发生。
但是,确实有效:
<?php
$html = "one の two の";
$html = preg_replace('/の/s', 'shazam!!', $html);
file_put_contents('output.html', $html);
?>
输出为:
one shazam!! two shazam!!
有什么建议吗?我知道这是一个字符编码问题,但我似乎无法让它正常工作。
更新:
这是一个修改后的版本,用于测试 $html 变量上的 UTF-8 编码:
<?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, FILE_TEXT, $context);
if(mb_detect_encoding($html, 'UTF-8')) print("Yep, UTF-8 it is.\n");
if(! mb_detect_encoding($html, 'UTF-8', true)) print("Well, on second thought.. maybe not!\n");
?>
输出为:
Yep, UTF-8 it is.
Well, on second thought.. maybe not!
我的解决方案
这是我想出的一种解决方案:
<?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('/の/su', 'shazam!!', $html);
(使用 /u UTF-8 修饰符)。
更新:
当使用 /u 修饰符时,输入文本(此处为 $html)必须是有效的 UTF-8,否则根本不会替换任何内容。输入文本编码可以通过mb_check_encoding($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);
?>
我得到"shazam!!"
我正在尝试编写一个 PHP 脚本来将日本烹饪食谱从日语翻译成英语。这是宠物项目,不一定是完美的。我的战术是..
- 使用file_get_contents 获取网站内容
- 用英文替换一些日语(主要是成分名称)
- 将结果写入 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);
?>
我正在使用 Sublime Text 编辑文件 (translate.php),并且我已确保使用以下方式保存文件:文件/使用编码保存/UTF-8
当我运行脚本时,除了の替换外,一切正常。没有替换の发生。
但是,确实有效:
<?php
$html = "one の two の";
$html = preg_replace('/の/s', 'shazam!!', $html);
file_put_contents('output.html', $html);
?>
输出为:
one shazam!! two shazam!!
有什么建议吗?我知道这是一个字符编码问题,但我似乎无法让它正常工作。
更新:
这是一个修改后的版本,用于测试 $html 变量上的 UTF-8 编码:
<?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, FILE_TEXT, $context);
if(mb_detect_encoding($html, 'UTF-8')) print("Yep, UTF-8 it is.\n");
if(! mb_detect_encoding($html, 'UTF-8', true)) print("Well, on second thought.. maybe not!\n");
?>
输出为:
Yep, UTF-8 it is.
Well, on second thought.. maybe not!
我的解决方案
这是我想出的一种解决方案:
<?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('/の/su', 'shazam!!', $html);
(使用 /u UTF-8 修饰符)。
更新:
当使用 /u 修饰符时,输入文本(此处为 $html)必须是有效的 UTF-8,否则根本不会替换任何内容。输入文本编码可以通过mb_check_encoding($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);
?>
我得到"shazam!!"