Tuesday, 27 October 2015

Regex remove html attributes from tags

You can remove any attribute from html tag using PHP regex

<?php

$html_content = '<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. <a href="http://www.lipsum.com/">Fusce</a>
Sit amet semper elit. <a href="http://krupalpatel92.blogspot.in/">In faucibus</a> in nulla a aliquam. Nulla ultricies eget erat non bibendum. Pellentesque a pellentesque mauris. Etiam purus urna, porttitor ut massa non, vehicula pretium nulla. Donec venenatis finibus nunc vitae euismod. Morbi auctor venenatis aliquet. Etiam consequat sapien efficitur augue tincidunt rutrum</p>';

$attr_name = "href";

$content = preg_replace('/(<[^>]+) '.$attr_name.'=".*?"/i', '$1', $html_content);
echo $content;

?>

No comments:

Post a Comment