在 PHP 中将<跨度样式=“字体-粗细:粗体”>某些文本<跨度>转换为<b>某些文本</b>

作为标题,如果我有一些html,我想去掉样式标签并将它们转换为html标签,这样才能使它成为.我怎样才能在 PHP 中做到这一点?<p><span style="font-style:italic">abcde</span><span style="font-weight:bold">abcde</span></p><p><i>abcde</i><b>abcde</b></p>

我注意到,当我在CK编辑器中打开html时,这种转换是自动完成的。但我想在后端PHP中做到这一点。谢谢。


慕少森
浏览 112回答 3
3回答

达令说

$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){&nbsp; &nbsp; foreach($results as $result){&nbsp; &nbsp; &nbsp; &nbsp; $style = $result->getAttribute("style");&nbsp; &nbsp; &nbsp; &nbsp; $style_arr = explode(";",$style);&nbsp; &nbsp; &nbsp; &nbsp; $style_template = '%s';&nbsp; &nbsp; &nbsp; &nbsp; if(count($style_arr)>0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach($style_arr as $style_item){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($style_item == 'font-style-italic'){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $style_template = '<i>'.$style_template.'</i>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($style_item == 'font-weight:bold'){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $style_template = '<b>'.$style_template.'</b>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $str .= sprintf($style_template,$result->nodeValue);&nbsp; &nbsp; }}$str = '<p>'.$str.'</p>';

长风秋雁

您还可以在 php 参数下使用 html 标记,或者像这样使用 php 开始和结束标记<?php&nbsp; &nbsp; echo"<h1>Here is Heading h1 </h1>";&nbsp;&nbsp;?>或者你可以把你的html代码放在“ ”后回声像这样<?php&nbsp;echo"Your Html Code Here";&nbsp; &nbsp; &nbsp; ?>

开心每一天1111

$output&nbsp;=&nbsp;preg_replace('/(<[^>]+)&nbsp;style=".*?"/i',&nbsp;'$1',&nbsp;$input);匹配一个跟随一个或多个,直到空间来临并且到达。将使用大写样式,如果标记不包含 ,则将标记保留原样。对于单引号,请使用以下内容:<>style="anything"/i$1style=""style=''(<[^>]+)&nbsp;style=("|').*?("|')
打开App,查看更多内容
随时随地看视频慕课网APP