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!