How can I use substr() in a HTML dom? I want keep all the html tags and just shorten the text.
$str = '<div><a href="http://www.weather.com">Today is a nice day</a></div>';
$part1 = preg_replace("/<a(.*?)>(.*?)<\/a>/i",substr('\\2', 0,10),$str).'...';
$part2 = preg_replace("/<a(.*?)>(.*?)<\/a>/i",'\\2',$str);
echo str_replace($part2,$part1,$str);// nothing change
// I need <div><a href="http://www.weather.com">Today is a...</a></div>
It could probably be a bit more robust, but this should do the trick: