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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
int RemoveHtmlTags (const char *html, char *unhtml)
{
int err;
regex_t preg;
int start = 0;
int end = 0;
// Regular expression
const char *str_regex = ">([^<])+";
char *temp = NULL;
char *rest = NULL;
temp = (char*)malloc((strlen(html)+1)*sizeof(char));
rest = (char*)malloc((strlen(html)+1)*sizeof(char));
strcpy (rest, html);
// Regex compilation
err = regcomp (&preg, str_regex, REG_EXTENDED);
if (err == 0)
{
int match = 0;
size_t nmatch = 0;
regmatch_t *pmatch = NULL;
nmatch = preg.re_nsub;
pmatch = malloc (sizeof (*pmatch) * nmatch);
// Memory allocation for pmatch
while (match == 0)
{
// Matching to find no html text
match = regexec (&preg, rest, nmatch, pmatch, 0);
// Calculate the substring size
if (match == 0)
{
start = pmatch[0].rm_so + sizeof(char);
end = pmatch[0].rm_eo;
size_t size = end - start;
//printf("%s\n",&rest[start]);
strncat (temp, &rest[start], size);
strcat (temp, " ");
strcpy (rest, &rest[end]);
}
}
free (pmatch);
strcpy(unhtml,temp);
}
else
{
fprintf (stderr, "Regcomp\n");
exit (EXIT_FAILURE);
}
free (temp);
temp = NULL;
free(rest);
rest = NULL;
regfree (&preg);
return 0;
} |
Partager