Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm attempting to automatically translate the english parts of a HTML document into another language using PHP. I have a translation API up and running and I now need to extract all text, translate it and re-integrate it into its original position in the DOM.

I would like to translate all text on the page with one API call so I am thinking of something like the following:

$originalText = preg_replace('#\<(.*?)\>#', '[:]', $html); //replace all html tags with [:] markers
$newText = translate($originalText, 'EN', 'DE'); //translate to german via my API
$newText = explode('[:]',$newText); //explode the text into an array
//...dang I'm stuck!

I'm looking for suggestions to get the text from the array back into its original position in the html code. Alternatively, if there's a better way to approach this - I'm all ears!

share
add comment (requires an account with 50 reputation)

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.