Jump to content

Handling special characters in data?


Strahan

Recommended Posts

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;
  }

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.