Jump to content

regex and google


yolop

Recommended Posts

Hello everyone

Is there a Web site that I was getting him a text and what word I want to take from the text  and the web site create the Regex's this? Free?

 

Question 2) How can I send information through PHP Translate.google.com and get back to the translation

 

thank

Link to comment
Share on other sites

Question 2) How can I send information through PHP Translate.google.com and get back to the translation

 

thank

 

If you are talking about using the google translator on your website I have made a script just for you using curl. Before this post I knew nothing about curl but here it is. First define/put the below function in your header or at the top of the page.

function translate($sentence,$languagefrom,$languageto)
{
$curl_handle=curl_init('http://translate.google.com/translate_t');
//curl_setopt($curl_handle,CURLOPT_URL,'http://example.com');
curl_setopt($curl_handle, CURLOPT_HEADER, false);
curl_setopt($curl_handle, CURLOPT_FAILONERROR, true);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); // request as if Firefox    
curl_setopt($curl_handle, CURLOPT_POST, true);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, 'prev=&hl=en&ie=UTF-8&text='.$sentence.'&sl='.$languagefrom.'&tl='.$languageto);
curl_setopt($curl_handle, CURLOPT_NOBODY, false);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);

$buffer=strip_tags($buffer,'<div>');
preg_match_all("/<div id=result_box dir=\"ltr\">[^<]+<\/div>/",$buffer,$match);
return $match[0][0];
}

Now to lets say convert English to Chinese use the following:

echo translate('This is the text','en','zh-CN');

You will notice the languages are not the name of the language, they are abbrievations. That is just how google define their $_POST input variables. So the conversion of the abbrievation to language that you can use in any of those two fields are as follows:

ar = Arabic
bg = Bulgarian
ca = Catalan
zh-CN = Chinese
hr = Croatian
cs = Czech
da = Danish
nl = Dutch
en = English
tl = Filipino
fi = Finnish
fr = French
de = German
el = Greek
iw = Hebrew
hi = Hindi
id = Indonesian
it = Italian
ja = Japanese
ko = Korean
lv = Latvian
lt = Lithuanian
no = Norwegian
pl = Polish
pt = Portuguese
ro = Romanian
ru = Russian
sr = Serbian
sk = Slovak
sl = Slovenian
es = Spanish
sv = Swedish
uk = Ukrainian
vi = Vietnamese

Link to comment
Share on other sites

and what about hebrew????

 

and

i wrote

<?php

 

function translate($sentence,$languagefrom,$languageto)

{

$curl_handle=curl_init('http://translate.google.com/translate_t');

//curl_setopt($curl_handle,CURLOPT_URL,'http://example.com');

curl_setopt($curl_handle, CURLOPT_HEADER, false);

curl_setopt($curl_handle, CURLOPT_FAILONERROR, true);

curl_setopt($curl_handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); // request as if Firefox   

curl_setopt($curl_handle, CURLOPT_POST, true);

curl_setopt($curl_handle, CURLOPT_POSTFIELDS, 'prev=&hl=en&ie=UTF-8&text='.$sentence.'&sl='.$languagefrom.'&tl='.$languageto);

curl_setopt($curl_handle, CURLOPT_NOBODY, false);

curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);

curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);

$buffer = curl_exec($curl_handle);

curl_close($curl_handle);

 

$buffer=strip_tags($buffer,'<div>');

preg_match_all("/<div id=result_box dir=\"ltr\">[^<]+<\/div>/",$buffer,$match);

return $match[0][0];

}

echo translate('This is the text','en','zh-CN');

 

 

?>

 

and this not write anything

Link to comment
Share on other sites

and what about hebrew????

 

...

 

and this not write anything

I just tried the script again and it didn't work on me. So I tried visiting http://www.translate.google.com/ and translate the same sentence from and to the same language and as the translation I recieved just blank binary box symbols. So google may be working on the Chinese translator but the following will work as a function call (as of when this post was made):

echo translate('This is the text','en','fr');

Link to comment
Share on other sites

I just discovered the bug when converting to Hebrew. There was a html variable dir= which I have now adapted the script to for you. So use the following code instead:

<?php

function translate($sentence,$languagefrom,$languageto)
{
$curl_handle=curl_init('http://translate.google.com/translate_t');
curl_setopt($curl_handle, CURLOPT_HEADER, false);
curl_setopt($curl_handle, CURLOPT_FAILONERROR, true);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); // request as if Firefox   
curl_setopt($curl_handle, CURLOPT_POST, true);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, 'prev=&hl=en&ie=UTF-8&text='.$sentence.'&sl='.$languagefrom.'&tl='.$languageto);
curl_setopt($curl_handle, CURLOPT_NOBODY, false);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);

$buffer=strip_tags($buffer,'<div>');
//below line changed
preg_match_all("/<div id=result_box dir=\"[^\"]+\">[^<]+<\/div>/",$buffer,$match);
return $match[0][0];
}

echo translate('This is the text','en','iw');
?>

Hope that helps.

Link to comment
Share on other sites

I just discovered the bug when converting to Hebrew. There was a html variable dir= which I have now adapted the script to for you. So use the following code instead:

<?php

function translate($sentence,$languagefrom,$languageto)
{
$curl_handle=curl_init('http://translate.google.com/translate_t');
curl_setopt($curl_handle, CURLOPT_HEADER, false);
curl_setopt($curl_handle, CURLOPT_FAILONERROR, true);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); // request as if Firefox   
curl_setopt($curl_handle, CURLOPT_POST, true);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, 'prev=&hl=en&ie=UTF-8&text='.$sentence.'&sl='.$languagefrom.'&tl='.$languageto);
curl_setopt($curl_handle, CURLOPT_NOBODY, false);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);

$buffer=strip_tags($buffer,'<div>');
//below line changed
preg_match_all("/<div id=result_box dir=\"[^\"]+\">[^<]+<\/div>/",$buffer,$match);
return $match[0][0];
}

echo translate('This is the text','en','iw');
?>

Hope that helps.

still

i dont sww anything

Link to comment
Share on other sites

It seems google is playing hard to get. They keep on changing the source code of the translator. So the only way which will make it possible for a pernament translator script is to make a bot (easy) that retrieves the source code then use an artificial intelligence for to decide what to submit to the google server via curl and to retrieve from the results the translation. Although I have allready done the section for retrieving from the results the translation, a bot with an artificial intelligence will be needed to cope with googles ever changing code. So although the above scripts did work for me before, I will help find a way to make sure it works for more than a few days but will take some time.

Link to comment
Share on other sites

Google has a full API for their translator.

 

You can find it here:

 

http://code.google.com/apis/ajaxlanguage/

That is an ajax api and if you wanted to use the translation for server-side processing then using that ajax method, you would need to do something like write the translation to a mysql database for the server to read. Also what if a user hasn't got ajax/java enabled, then they wouldn't be able to view the translation. So that is why I am making a php (server side) script that will process that data for the user. I have been working hard on this and I manage to make a script that will automatically detect the hidden fields and include them into the server side post. So my new script (hopefully will last longer) is as follows:

<?php
function translate($sentence,$languagefrom,$languageto)
{
$homepage = file_get_contents('http://translate.google.com/translate_t');
if ($homepage==false) {$homepage='';}
preg_match_all("/<form[^>]+ction=\"\/translate_t\".*<\/form>/",$homepage,$botmatch);
$botmatch[0][0]=preg_replace("/<\/form>.*/",'',$botmatch[0][0]);
preg_match_all("/<input[^>]+>/",$botmatch[0][0],$botinput);
$id=0;
while (isset($botinput[0][$id]))
{
preg_match_all("/value=(\"|'|[^\"'])[^\"']+(\"|'|[^\"'])?( |>|	)/",$botinput[0][$id],$tmp);
$tmp[0][0]=preg_replace('/((\'|")?[^\'"]+)/','$1',$tmp[0][0]);
$tmp[0][0]=preg_replace('/(\'|")/','',$tmp[0][0]);
$tmp[0][0]=preg_replace('/.*value=/i','',$tmp[0][0]);
$len=strlen($tmp[0][0]);
$len-=1;
$tmp[0][0]=substr($tmp[0][0],0,$len);

preg_match_all("/name=(\"|'|[^\"'])[^\"']+(\"|'|[^\"'])?( |>|	)/",$botinput[0][$id],$tmpname);
$tmpname[0][0]=preg_replace('/((\'|")?[^\'"]+)/','$1',$tmpname[0][0]);
$tmpname[0][0]=preg_replace('/(\'|")/','',$tmpname[0][0]);
$tmpname[0][0]=preg_replace('/.*name=/i','',$tmpname[0][0]);
$len=strlen($tmpname[0][0]);
$len-=1;
$tmpname[0][0]=substr($tmpname[0][0],0,$len);

if (strlen($tmpname[0][0])>0 && !in_array($tmpname[0][0],array('text','sl','tl')))
	{
	$vars.=$tmpname[0][0]."=".$tmp[0][0].'&';
	}
unset($tmp);
unset($tmpname);
$id+=1;
}
$curl_handle=curl_init('http://translate.google.com/translate_t');
curl_setopt($curl_handle, CURLOPT_HEADER, true);
curl_setopt($curl_handle, CURLOPT_FAILONERROR, true);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); // request as if Firefox   
curl_setopt($curl_handle, CURLOPT_POST, true);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $vars.'text='.$sentence.'&sl='.$languagefrom.'&tl='.$languageto);
curl_setopt($curl_handle, CURLOPT_NOBODY, false);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,false);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);

$buffer=strip_tags($buffer,'<div>');
preg_match_all("/\<div id\=result_box dir\=\"[^\"]+\"\>[^<]+\<\/div\>/",$buffer,$match);
$match[0][0]=strip_tags($match[0][0]);
return $match[0][0];
}



echo translate('This is the text','en','iw');
?>

Good luck. And just let me know if it stops working because I would like to use the same script too.

Link to comment
Share on other sites

Google has a full API for their translator.

 

You can find it here:

 

http://code.google.com/apis/ajaxlanguage/

That is an ajax api [...]

 

There is no reason why PHP couldn't call the API. You'll just need to check the JS code they provide and find out the URL to the API. Then you can do like this:

 

<?php
function translate($textSource, $langSource, $langTarget)
{
$ch = curl_init();
curl_setopt_array($ch, array(
	CURLOPT_URL => 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=' . urlencode($textSource) . '&langpair=' . urlencode($langSource . '|' . $langTarget),
	CURLOPT_RETURNTRANSFER => true
));

$ret = json_decode(curl_exec($ch), true);
curl_close($ch);

if ($ret['responseStatus'] != '200') {
	throw Exception('Translation failed.');
}

return $ret['responseData']['translatedText'];
}

echo translate('hola', 'es', 'en');
?>

Link to comment
Share on other sites

Google has a full API for their translator.

 

You can find it here:

 

http://code.google.com/apis/ajaxlanguage/

That is an ajax api and if you wanted to use the translation for server-side processing then using that ajax method, you would need to do something like write the translation to a mysql database for the server to read. Also what if a user hasn't got ajax/java enabled, then they wouldn't be able to view the translation. So that is why I am making a php (server side) script that will process that data for the user. I have been working hard on this and I manage to make a script that will automatically detect the hidden fields and include them into the server side post. So my new script (hopefully will last longer) is as follows:

<?php
function translate($sentence,$languagefrom,$languageto)
{
$homepage = file_get_contents('http://translate.google.com/translate_t');
if ($homepage==false) {$homepage='';}
preg_match_all("/<form[^>]+ction=\"\/translate_t\".*<\/form>/",$homepage,$botmatch);
$botmatch[0][0]=preg_replace("/<\/form>.*/",'',$botmatch[0][0]);
preg_match_all("/<input[^>]+>/",$botmatch[0][0],$botinput);
$id=0;
while (isset($botinput[0][$id]))
{
preg_match_all("/value=(\"|'|[^\"'])[^\"']+(\"|'|[^\"'])?( |>|	)/",$botinput[0][$id],$tmp);
$tmp[0][0]=preg_replace('/((\'|")?[^\'"]+)/','$1',$tmp[0][0]);
$tmp[0][0]=preg_replace('/(\'|")/','',$tmp[0][0]);
$tmp[0][0]=preg_replace('/.*value=/i','',$tmp[0][0]);
$len=strlen($tmp[0][0]);
$len-=1;
$tmp[0][0]=substr($tmp[0][0],0,$len);

preg_match_all("/name=(\"|'|[^\"'])[^\"']+(\"|'|[^\"'])?( |>|	)/",$botinput[0][$id],$tmpname);
$tmpname[0][0]=preg_replace('/((\'|")?[^\'"]+)/','$1',$tmpname[0][0]);
$tmpname[0][0]=preg_replace('/(\'|")/','',$tmpname[0][0]);
$tmpname[0][0]=preg_replace('/.*name=/i','',$tmpname[0][0]);
$len=strlen($tmpname[0][0]);
$len-=1;
$tmpname[0][0]=substr($tmpname[0][0],0,$len);

if (strlen($tmpname[0][0])>0 && !in_array($tmpname[0][0],array('text','sl','tl')))
	{
	$vars.=$tmpname[0][0]."=".$tmp[0][0].'&';
	}
unset($tmp);
unset($tmpname);
$id+=1;
}
$curl_handle=curl_init('http://translate.google.com/translate_t');
curl_setopt($curl_handle, CURLOPT_HEADER, true);
curl_setopt($curl_handle, CURLOPT_FAILONERROR, true);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); // request as if Firefox   
curl_setopt($curl_handle, CURLOPT_POST, true);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $vars.'text='.$sentence.'&sl='.$languagefrom.'&tl='.$languageto);
curl_setopt($curl_handle, CURLOPT_NOBODY, false);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,false);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);

$buffer=strip_tags($buffer,'<div>');
preg_match_all("/\<div id\=result_box dir\=\"[^\"]+\"\>[^<]+\<\/div\>/",$buffer,$match);
$match[0][0]=strip_tags($match[0][0]);
return $match[0][0];
}



echo translate('This is the text','en','iw');
?>

Good luck. And just let me know if it stops working because I would like to use the same script too.

stiill

i dont see anything

Link to comment
Share on other sites

Well I have tested the script again and for me it works. So as Daniel said it could be just that you haven't got curl but then again you should get a fatal error if that is the case. So try setting up a page with the following test script to test your curl version to see what you can see. With the below script your should be able to see the google translator homepage and your curl version information at the bottom. If you can't see that then it is not the script that is failing but rather the php.ini extensions configuration that is failing. Also be sure that you have php version 5 as the file_get_contents function is only available in php5.

<?
$curl_handle=curl_init('http://translate.google.com/translate_t');
curl_setopt($curl_handle, CURLOPT_HEADER, false);
curl_setopt($curl_handle, CURLOPT_FAILONERROR, true);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); // request as if Firefox   
curl_setopt($curl_handle, CURLOPT_POST, false);
curl_setopt($curl_handle, CURLOPT_NOBODY, false);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,false);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
echo $buffer."<xmp>";
var_dump(curl_version());
echo "</xmp>";
?>

Let me know of the results of the above test script.

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.