typedef struct ElementAttribute ElementAttribute;
struct ElementAttribute
{
const char* name;
const char* value;
ElementAttribute* previous;
};
static const char* HtmlFindSpaceFromRecursion(const char* str, const char* start)
{
const char* p = str;
while (p >= start)
{
if (*p == 0x20)
{
return p;
}
--p;
}
return str;
}
/**
* 1.注意,这个函数只适合标准的属性格式,也就是"是成对存在.
*/
static ElementAttribute* HtmlGetElementAttribute(char* str)
{
ElementAttribute *attr = NULL;
static char quote = '\"';
char* p = strchr(str, quote);
while (p)
{
char *start = p;
//属性名
char* a_name_start = (char*) HtmlFindSpaceFromRecursion(start, str);
*(start - 1) = 0;
//属性值
p = strchr(start + 1, quote);
*p = 0;
ElementAttribute *next = (ElementAttribute*) malloc(
sizeof(ElementAttribute));
memset(next, 0, sizeof(ElementAttribute));
next->name = a_name_start + 1;
next->value = start + 1;
next->previous = attr;
attr = next;
p = strchr(p + 1, quote);
}
return attr;
}
TEST(main,testElementAttribute)
{
char* str = strdup("p height=\"1em\" width=\"0pt\" align=\"center\"");
ElementAttribute* attr = HtmlGetElementAttribute(str);
while(attr)
{
QXLOG("name: ",attr->name);
QXLOG("value:",attr->value);
attr = attr->previous;
}
free(str);
}[C/C++]_[字符串处理]_[过滤出HTML标签的属性值]
最新推荐文章于 2024-04-02 17:40:47 发布
本文详细解析了一个用于从HTML字符串中提取元素属性的函数,包括如何处理属性名称和值,以及如何遍历整个字符串来收集所有属性。
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
ACE-Step
音乐合成
ACE-Step
ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言
3万+

被折叠的 条评论
为什么被折叠?



