Strahan Posted June 5, 2010 Share Posted June 5, 2010 Hi. My friend was amusing herself taking her emails, using Babelfish to translate them to other languages then translate them back to English and giggling at the weird wording. I figured I'd make it easier by writing a little page to do the legwork. I went to babelfish.altavista.com and viewed source. It seems the meat of the translation form is: <form action="http://babelfish.yahoo.com/translate_txt" method="POST" onSubmit="return verifyTrText();"; name="frmTrText"> <input type=hidden name="ei" value="UTF-8"> <input type=hidden name=doit value="done"> <input type=hidden name=fr value="bf-home"> <input type=hidden name=intl value="1"> <input type=hidden name=tt value="urltext" > <textarea rows="6" wrap=virtual cols="20" name="trtext""></textarea><br> <nobr><select name="lp" class="inp_sel" style="font-size:0.8em;"> <option value="">Select from and to languages</option> (... bunch of language opts stricken for size ...) </select></nobr> <input class="inp_btn" type="Submit" value="Translate" name="btnTrTxt"> </form> So to test I built a querystring based URL to try to translate "hello" to German: http://babelfish.yahoo.com/translate_txt?ei=UTF-8&doit=done&fr=bf-res&intl=1&tt=urltext&trtext=hello&lp=en_de Voila, worked great. Dug out the positioning in the file and wrote the code to get the foreign word then translate back to English. It works great for some stuff, but then it dies on others. I copied the foreign->english URL it made and found that if I paste it to Babelfish, I get nothing. Turns out if I remove the special chars in the string like %FC and stuff, it works fine. So apparently it's choking when trying to put the special accented stuff like umlauts in the querystring. Anyone know of a way to properly stick it in there, or is the problem on babelfish's side since they're expecting post data not get data? Does that even make a difference? Thanks My code: <?php if (empty($_REQUEST["text"])) { echo "<form name=data method=post action=trans.php>Language base:<select name=lang>"; echo "<option value='en_zh'>Chinese-simp</option>"; echo "<option value='en_zt'>Chinese-trad</option>"; echo "<option value='en_nl'>Dutch</option>"; echo "<option value='en_fr'>French</option>"; echo "<option value='en_de'>German</option>"; echo "<option value='en_el'>Greek</option>"; echo "<option value='en_it'>Italian</option>"; echo "<option value='en_ja'>Japanese</option>"; echo "<option value='en_ko'>Korean</option>"; echo "<option value='en_pt'>Portuguese</option>"; echo "<option value='en_ru'>Russian</option>"; echo "<option value='en_es'>Spanish</option>"; echo "</select><BR><HR><textarea name=text rows=5 cols=80></textarea><BR><BR>"; echo "<input type=submit value='Transmangle'>"; } else { $data = file_get_contents("http://babelfish.yahoo.com/translate_txt?ei=UTF-8&doit=done&fr=bf-res&intl=1&tt=urltext&trtext=" . str_replace(" ", "%20", $_REQUEST["text"]) . "&lp=" . $_REQUEST["lang"]); $foreign = substr(substr($data, strpos($data, "<div style=\"padding:0.6em;\">"), strpos($data, "<div style=\"padding:0.6em;\">")-strpos($data, "</div>")), strpos(substr($data, strpos($data, "<div style=\"padding:0.6em;\">"), strpos($data, "<div style=\"padding:0.6em;\">")-strpos($data, "</div>")), ">")+1, strpos(substr($data, strpos($data, "<div style=\"padding:0.6em;\">"), strpos($data, "<div style=\"padding:0.6em;\">")-strpos($data, "</div>")), "</div>")-(strpos(substr($data, strpos($data, "<div style=\"padding:0.6em;\">"), strpos($data, "<div style=\"padding:0.6em;\">")-strpos($data, "</div>")), ">")+1)); echo $foreign . "<BR>"; echo "http://babelfish.yahoo.com/translate_txt?ei=UTF-8&doit=done&fr=bf-res&intl=1&tt=urltext&trtext=" . urlencode($foreign) . "&lp=" . substr($_REQUEST["lang"], 3, 2) . "_en<BR><BR>"; $data = file_get_contents("http://babelfish.yahoo.com/translate_txt?ei=UTF-8&doit=done&fr=bf-res&intl=1&tt=urltext&trtext=" . str_replace(" ", "%20", $foreign) . "&lp=" . substr($_REQUEST["lang"], 3, 2) . "_en"); $english = substr(substr($data, strpos($data, "<div style=\"padding:0.6em;\">"), strpos($data, "<div style=\"padding:0.6em;\">")-strpos($data, "</div>")), strpos(substr($data, strpos($data, "<div style=\"padding:0.6em;\">"), strpos($data, "<div style=\"padding:0.6em;\">")-strpos($data, "</div>")), ">")+1, strpos(substr($data, strpos($data, "<div style=\"padding:0.6em;\">"), strpos($data, "<div style=\"padding:0.6em;\">")-strpos($data, "</div>")), "</div>")-(strpos(substr($data, strpos($data, "<div style=\"padding:0.6em;\">"), strpos($data, "<div style=\"padding:0.6em;\">")-strpos($data, "</div>")), ">")+1)); echo $english; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/203926-handling-special-characters-in-data/ Share on other sites More sharing options...
jskywalker Posted June 5, 2010 Share Posted June 5, 2010 i think problem is here: $data = file_get_contents("http://babelfish.yahoo.com..... in my opinion it should be something as (untested): $url = ""http://babelfish.yahoo.com/translate_txt"; $d = ei='UTF-8&doit=done&fr=bf-res&intl=1&tt=urltext&trtext=" . str_replace(" ", "%20", $_REQUEST["text"]) . "&lp=" . $_REQUEST["lang"]'; $data = http_post_data($url, $d); or you could try a translate site which has implemented a proper API: http://googleajaxsearchapi.blogspot.com/2008/03/introducing-ajax-language-api-tools-for.html Quote Link to comment https://forums.phpfreaks.com/topic/203926-handling-special-characters-in-data/#findComment-1068158 Share on other sites More sharing options...
Strahan Posted June 6, 2010 Author Share Posted June 6, 2010 Thanks. I tried to mess around with the http_post_data method but it didn't like it. Your recommendation to use Google API worked good though. However, Google API is apparently quite a capable translator. Taking stuff from English -> German -> English resulted in pretty much the same text. Pretty cool (but unhelpful for me lol). I ended up making it go English -> German -> French -> Spanish -> Afrikaans -> Irish -> Swedish -> English to get the same funny effect as Babelfish hehe. Quote Link to comment https://forums.phpfreaks.com/topic/203926-handling-special-characters-in-data/#findComment-1068431 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.