Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify all links in HTML Purifier

I am using HTML purifier to remove all unnecessary/malicious html tags.

$html = 'dirty html provided by user';
$config = HTMLPurifier_Config::createDefault();
$config->set('HTML.Allowed', 'p,a[href], ... other tags);
$purifier = new HTMLPurifier($config);;
$output = $purifier->purify($html);

It works really nice, but I want to do a little bit more. I want to change all my <a href='link'>...</a> to something else like <a href='somefunc(link)' rel="nofollow" target="_blank"> ... </a>.

After searching for a little bit, I found the following relevant link, but the problem is that it requires patching a complex library (which is not really a good idea, also the solution is kind of complicated).

Reading through their forum post, it looks like there is solution for adding nofollow parameter is $config->set("HTML.Nofollow", true);, but I still fail to find how can modify every link.

My current solution is to parse purified html by myself and to modify a link, but I think that there is a way to do this through HTML Purifier.

like image 807
Salvador Dali Avatar asked Dec 19 '22 17:12

Salvador Dali


1 Answers

Actually I found partial solution on one of the links on the forum.

This is what I need to do:

$config->set('HTML.Nofollow', true);
$config->set('HTML.TargetBlank', true);

So the full thing looks like this:

$config = HTMLPurifier_Config::createDefault();
$config->set('HTML.Nofollow', true);
$config->set('HTML.TargetBlank', true);
$config->set('HTML.Allowed', 'a,b,strong,i,em,u');
$purifier = new HTMLPurifier($config);
like image 88
Salvador Dali Avatar answered Dec 24 '22 00:12

Salvador Dali