作为标题,如果我有一些html <p><span style="font-style:italic">abcde</span><span style="font-weight:bold">abcde</span></p>
,我想剥离样式标签并将其转换为html标签,以使其成为<p><i>abcde</i><b>abcde</b></p>
。如何在PHP中做到这一点?
我注意到当我在CKEditor中打开html时,这种转换是自动完成的。但是我想在后端PHP中做到这一点。谢谢。
作为标题,如果我有一些html
abcde
abcde,我要剥离样式标签,然后...$string = '<p><span style="font-style-italic;font-weight:bold">abcde</span><span style="font-weight:bold">abcde</span></p>';
$dom = new DOMDocument();
$dom->loadHTML($string);
$xp = new DOMXPath($dom);
$str = '';
$results = $xp->query('//span');
if($results->length>0){
foreach($results as $result){
$style = $result->getAttribute("style");
$style_arr = explode(";",$style);
$style_template = '%s';
if(count($style_arr)>0){
foreach($style_arr as $style_item){
if($style_item == 'font-style-italic'){
$style_template = '<i>'.$style_template.'</i>';
}
if($style_item == 'font-weight:bold'){
$style_template = '<b>'.$style_template.'</b>';
}
}
}
$str .= sprintf($style_template,$result->nodeValue);
}
}
$str = '<p>'.$str.'</p>';
您还可以在php参数下使用html标签,或在php中使用像这样的打开和关闭标签
$output = preg_replace('/(<[^>]+) style=".*?"/i', '$1', $input);