Skip to content

Instantly share code, notes, and snippets.

@ifduyue
Created January 13, 2012 02:37
Show Gist options
  • Save ifduyue/1604301 to your computer and use it in GitHub Desktop.
Save ifduyue/1604301 to your computer and use it in GitHub Desktop.
c++ strip_tags replace_all
string &replace_all(string &content, const string &from, const string &to)
{
size_t i = 0;
size_t j;
while((j = content.find(from, i)) != string::npos) {
content.replace(j, from.size(), to);
i = j + to.size();
}
return content;
}
string &strip_tags(string &html)
{
bool inflag = false;
bool done = false;
size_t i, j;
while (!done) {
if (inflag) {
i = html.find('>');
if (i != string::npos) {
inflag = false;
html.erase(0, i+1);
}
else {
done = true;
html.erase();
}
}
else {
i = html.find('<');
if (i != string::npos) {
j = html.find('>');
if (j == string::npos) {
inflag = true;
done = true;
html.erase(i);
}
else {
html.erase(i, j-i+1);
}
}
else {
done = true;
}
}
}
return html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment