对There a quick way to add prefixes to all CSS classes & ID's?
的附件类型我需要查找和替换字符串,如:
id="xyz"
将它们替换为id =“prefix_xyz”
这变得很棘手:
class="one two three"
他们都需要一个前缀。有任何想法吗?谢谢!
答案 0 :(得分:2)
通过搜索和替换所有id
和class
值Demo来执行此操作:
$dom = new DomDocument();
$dom->loadHTML($html);
$xpath = new DomXpath($dom);
$res = $xpath->query('//@class|//@id');
foreach($res as $attr)
{
$value = explode(' ', $attr->value);
foreach($value as &$set)
{
$set = 'prefix_'.trim($set);
}
unset($set);
$attr->value = implode(' ', $value);
}
echo $dom->saveHTML();