views:

144

answers:

3

Is there any API (like google translation api) in PHP which allows to translate HTML blocks and translate only text out of the html ?

A: 

Use Regular Expressions, PHP supports them. Regexlib has a nice library of various pre-written regexes you can adapt.

Xorlev
1. that was not the answer to his question 2. if you are saying create the api yourself, surely he should use a html parser.
Moak
don't parse html with regex please :) http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html
Bala Clark
A: 

Could not get your question perfectly but if you are looking to get text out of html, sort of crawling text on a site, you need a php crawler script:

http://forums.digitalpoint.com/showthread.php?t=708122

But if meant to strip html and get only text, you can use PHP's:

strip_tags function.

But if you meant to get specific piece of text, you could use regular expressions:

http://www.regular-expressions.info/

Sarfraz
+2  A: 

Microsoft's translation API will translate while maintaining HTML tags.

The API is documented here. It has both a REST and WSDL interface.

I tend to use the WSDL interface with PHP's SoapClient library. Here is some code to show you how to use it.

$client = new SoapClient("http://api.microsofttranslator.com/V1/SOAP.svc");

$params = array(
    'appId' => 'my_app_id', 
    'text' => '<p>This is a <b>test</b></p>', 
    'from' => 'en', 
    'to' => 'fr');

$translation = $client->translate($params);

var_dump($translation);

You'll need to register with Microsoft for your own application ID which you pass up with each request. You can register here.

I would advise against stripping out tags, translating and then re-inserting the tags. Since you have no guarantee that word number and order is preserved in the translation it makes it very difficult to know where to place the tags in the translated text. Better to have the MT engine handle the tags.

Stephen Curran
I'm very surprised I haven't seen the Microsoft Translator before, this looks really useful.
Bala Clark
Yep. It doesn't seem to be promoted as widely as the Google service. I don't know why.
Stephen Curran