Pacific-Design.com

    
Home Index

1. Cpp 11

2. Highlighter

Cpp 11 / Highlighter /

Search term highlighter - boost replace all

#include <iostream>
#include <string>

#include <boost/algorithm/string.hpp>
#include <boost/tokenizer.hpp>
#include <boost/foreach.hpp>

using namespace boost::algorithm;
using namespace std;
using namespace boost;

void highlighter(string terms, string text) {

    char_separator<char> sep(" ");
    tokenizer<char_separator<char> > tokens(terms, sep);

    BOOST_FOREACH(string term, tokens) {
        boost::replace_all(text, term, "<b>" + term + "</b>");
    }   
    cout << text << endl;
}

int main(int argc, char **argv)
{
    cout << "Search term highlighter" << endl;
    string text("I love boost library, and this is a test of boost library! ");
    highlighter("love boost", text);
                                                                                                                      
}

Output

I love boost library, and this is a test of boost library!