wasinger / htmlpagedom

jQuery-inspired DOM manipulation extension for Symfony's Crawler
MIT License
346 stars 50 forks source link

Find a string in HTML and replace it with link #24

Open ahoiroman opened 7 years ago

ahoiroman commented 7 years ago

Hello,

I would like to use your class to search a html-code for specific human-readable strings (that are shown in the browser - no hidden HTML-stuff) and replace them with a link.

E.G.

I did that by using the simple HTML DOM parser and it has a massive overhead. Is it possible to use your class to solve this in an elegant way?

glensc commented 3 years ago

@ahoiroman perhaps share your current code, will give some ideas to others.

I would say (not tested) to do something like:

// find appropriate node, perhaps;
$tags = $crawler->filter('div');

// walk over the nodes
$tags->each(function (HtmlPageCrawler $node): void {
    $html = '<a href="test.html" title="test">test</a>';
    $node->replaceWith($html);
});