I'm using GTranslate which works great with plain text but sometimes raises errors with html tags - so I'd like to extract the text between > and <, translate it and replace the original text.
I've written the following code which uses strpos but would like a more elegant method with dom or preg_match but not sure how?
<?php
require("GTranslate.php");
$gt = new Gtranslate;
$english = "<tag>hello world</tag><tag2>hello again</tag2>";
echo "English: " . htmlentities($english) . "<br />";
$pos = strpos($english, ">");
$italian = "";
while ($pos>0) {
// Add the html tag to the Italian string
$italian .= substr($english,0,$pos+1);
// Remove tag from English string
$english = substr($english,$pos+1);
// Look for end tag
$pos = strpos($english,"<");
// Translate text in between > and <
$trans = substr($english,0,$pos);
if (!empty( $trans)) {
// Translate and add to Italian
$trans = $gt->en_to_it($trans);
}
$italian .= $trans;
// reduce English again
$english = substr($english,$pos);
// Look for next starting tag
$pos = strpos($english,">");
}
// Any remainding english text
$italian .= $english;
echo "Italian: " . htmlentities($italian) . "<br />";
?>