Skip navigation.

Log in | Sign up

photo of Anibal

Error Prone

_dy->connect(qObject, "post()", self, OBJC_SLOT(0, @selector(ohYea)));

Cortar un texto sin quitar las xhtml-tags

, ,

Éste es un pequeño código que demuestra lo desordenado que trabajo. Por lo general mi modus operandi es:
Tener una idea.
Codificarla hasta que anda (con todas las malas prácticas habidas y por haber).
Limpiarla hasta que quede más bonita y legible.
Aburrirme y abandonar el proyecto.

Lo bueno, es que la primer parte me divierte mucho y puedo pasar horas viendo, modificando, aprendiendo (sobre todo aprendiendo) y diciendome "pero que pelotudo, mirá lo que hice....".

Ahora si, el problema:


Tengo un texto largo (o corto no es el problema), yo quiero que los textos largos sean cortados hasta las primeras 256 letras, sin contar los tags de xhtml. O sea, si yo tengo un texto <strong>Anibal</strong>, éste tiene 6 letras. También lo que quiero es que el código resultante tenga todas sus tags bien cerradas (ésto, en el texto original está bien, asumo que no hay un texto que tenga alguna tag xhtml no cerrada).


El lenguaje objetivo para llevar a cabo ésta resolución es PHP, pero como a mi me rompe las... pelotas PHP, decidí hacer un algoritmo (no muy óptimo, no explotando ninguna facilidad del lenguaje) en C. Quedó ésto:
#include <stdio.h>
#include <string.h>

int main()
{
    char txt[] = "<strong>PALABRA</strong>estaba la pajara pinta sentada en el verde limon 
con <a href='#'>el pico <img src='aaa' /> tocaba <strong>la rama con la rama recoje la 
flor... hay hay hay, la chancha va pal mail, AHORA estaba aca,</strong> cuando estoy </a>mal 
cu<a href='#'>ando estoy solo, no necesito a nadie en que pensar, porque no se que hacer 
ai</a>";
    char txt2[] = "PALABRAestaba la pajara pinta sentada en el verde limon con el pico 
tocaba la rama con la rama recoje la flor... hay hay hay, la chancha va pal mail, AHORA 
estaba aca, cuando estoy mal cuando estoy solo, no necesito a nadie en que pensar, porque no 
se que hacer ai";

#define CORTE 160

    txt2[CORTE] = '\0';
    printf("referencia: %s\n", txt2);
    int i = 0;
    int count = 0;
    int len = strlen(txt);

    int tag_abiertas = 0;

    while(i < len && count < CORTE){
        if(txt[ i ] == '<'){
            i++;    // saltar <
            if(txt[ i ] == '/'){
                tag_abiertas--;
            }else{
                tag_abiertas++;
            }

            for(i; txt[ i ] != '>'; ++i);
            if(txt[i-1] == '/'){
                tag_abiertas--;
            }
            i++;    // saltar >
            continue;
        }
        count++;
        i++;
    }

    char aux[1024];

    printf("i = %d count = %d, tags_abiertas = %d\n", i, count, tag_abiertas);
    strncpy(aux, txt, i);
    aux[ i ] = '\0';
    printf("La frase formada es:\n\t%s\n", aux);

    int j = i;
    while(tag_abiertas != 0){
        printf("Quedaron tags abiertas\n");
        if(txt[ i ] == '<'){
            aux[j] = txt[ i ];
            i++;
            j++;
            if(txt[ i ] == '/'){
                tag_abiertas--;
            }else{
                tag_abiertas++;
            }

            for(i,j; txt[ i ] != '>'; ++i, j++){
                aux[j] = txt[ i ];
            }
            if(txt[i-1] == '/'){
                tag_abiertas--;
            }
            aux[j] = txt[ i ];    // asignar el >
            j++;i++;
            continue;
        }
        i++;
    }

    printf("Hola antes j = %d\n", j);
    aux[j] = '\0';
    printf("La frase formada es:\n\t%s\n", aux);
    return 0;
}

Probando Opera Mini 5 BetaEvangelion Manga

How to use Quote function:

  1. Select some text
  2. Click on the Quote link

Write a comment

Comment
(BBcode and HTML is turned off for anonymous user comments.)

If you can't read the words, press the small reload icon.


Smilies

Download Opera, the fastest and most secure browser
December 2009
M T W T F S S
November 2009January 2010
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31