Ver Mensaje Individual
  #1 (permalink)  
Antiguo 02/11/2011, 02:29
tonimito
 
Fecha de Ingreso: diciembre-2003
Mensajes: 157
Antigüedad: 20 años, 4 meses
Puntos: 1
DIVIDIR TEXTO PARTES IGUALES | Evitar cortar palabras y entre etiquetas HTML

Hola chic@s!
Recurro a vosotros, ya que me he quedado estancado.

Os explico, tengo ésta función, que lo que hace es conectarse al API Google Translate V2 y traducir textos. Hay un problema, si el texto a traducir es muy largo, hay que dividirlo, ya que por URL se puede pasar un máximo de 450 caracteres. Por tanto, lo que hago es dividir el texto en juegos de 450 caracteres y al final, lo vuelvo a juntar para su salida:

Código PHP:
function Translate($Texto$Idioma1$Idioma2$html boolean){
        
$Text $Texto;
        
$Key "Mi-API-Key";
        
$apiUrl "https://www.googleapis.com/language/translate/v2";
        
$From $Idioma1;
        
$To $Idioma2;
        if(
$html == true){
            
$Format 'html';
        }else{
            
$Format 'text';
        }
        if(
strlen($Text)>450){ //Detecto si el texto es superior a 450 caracteres
            
$TXT_Parts str_split($Text450); //Lo divido en partes iguales de 450
            
$get1 = array();
            
$get2 = array();
            
$url = array();
            
$TXT_Part = array();
            for(
$i=0$i<count($TXT_Parts); $i++){
                
$url[$i] = $apiUrl."?format=".$Format."&key=".$Key."&q=".urlencode($TXT_Parts[$i])."&source=".$From."&target=".$To;
                
$get1[$i] = file_get_contents($url[$i]);
                
$get2[$i] = json_decode($get1[$i]);
                foreach (
$get2[$i]->data->translations as $text) {
                     
$TXT_Part[$i] = $text->translatedText;
                }
            }
            return 
implode('',$TXT_Part); //Junto todas las partes resultantes
        
}else{
            
$url $apiUrl."?format=".$Format."&key=".$Key."&q=".urlencode($Text)."&source=".$From."&target=".$To;
            
$get1 file_get_contents($url);
            
$get2 json_decode($get1);
            foreach (
$get2->data->translations as $text) {
                  return 
$text->translatedText;
            }
        }
    } 
Todo funciona perfectamente, el problema es que ahora necesito:
  • Evitar partir palabras cuando parto el texto (1-> Hola como est | 2-> án ustedes).
  • Evitar partir palabras entre etiquetas HTML (<a></a>, <b></b>, etc)

Alguien tiene alguna idea?
Gracias a todos!!!
__________________
http://www.idilicstudio.com