简体   繁体   English

更换 <pre> 标签与

[英]Replace <pre> tags with <code>

What i need to do is to replace all pre tags with code tags. 我需要做的是用代码标签替换所有预标签。

Example

<pre lang="php">
    echo "test";
</pre>
Becomes
<code>
    echo "test";
</code>
<pre lang="html4strict">
    <div id="test">Hello</div>
</pre>
Becomes
<code>
    <div id="test">Hello</div>
</code>

And so on.. 等等..

Default DOM functions of php have a lot of problems because of the greek text inside. 由于内部包含希腊文字,因此php的默认DOM函数存在很多问题。
I think Simple HTML DOM Parser is what i need but i cant figure out how to do what i want. 我认为,简单HTML DOM解析器是我所需要的,但我无法弄清楚该怎么做。
Any ideas? 有任何想法吗?

UPDATE UPDATE
Im moving to a new CMS thats why im writing a script to format all posts to the correct format before inserting into DB. 我要迁移到新的CMS,这就是为什么我要在插入数据库之前编写脚本以将所有帖子格式化为正确的格式。 I cant use pre tags in the new CMS. 我不能在新的CMS中使用预标记。

Why not KISS (Keep It Simple, Stupid): 为什么不亲吻(保持简单,愚蠢):

echo str_replace(
    array('<pre>', '</pre>'),
    array('<code>', '</code>'),
    $your_html_with_pre_tags
);

Look at the manual . 看看手册 Changing <pre> tags to <code> should be as simple as: <pre>标签更改为<code>应该很简单:

$str = '<pre lang="php">
    echo "test";
</pre>
<pre lang="html4strict">
    <div id="test">Hello</div>
</pre>';
require_once("simplehtmldom/simple_html_dom.php");
$html = str_get_html($str);
foreach($html->find("pre") as $pre) {
    $pre->tag = "code";
    $pre->lang = null; // remove lang attribute?
}
echo $html->outertext;

// <code>
//     echo "test";
// </code>
// <code>
//     <div id="test">Hello</div>
// </code>

PS: you should encode the " , < and > characters in your input. PS:您应该在输入中编码"<>字符。

Just replacing pre tags with code tags changes the meaning and the rendering essentially and makes the markup invalid, if there are any block-level elements like div inside the element. 如果在元素内有任何块级元素(例如div ,则仅用code标签替换pre标签会实质上改变含义和渲染,并使标记无效。 So you need to revise your goal. 因此,您需要修改目标。 Check out whether you can actually keep using pre . 检查您是否实际上可以继续使用pre If not, use <div class=pre> instead, together with a style sheet that makes it behave like pre in rendering. 如果不是,请使用<div class=pre>以及样式表,使其表现得像pre渲染。 When you just replace pre tags with div tags, you won't create syntax errors (the content model of div allows anything that pre allows, and more). 当您仅用div标签替换pre标签时,就不会产生语法错误( div的内容模型允许pre允许的任何内容,以及更多)。

Regarding the lang attribute, lang="php" is incorrect (by HTML specs, lang attribute specifies the human language of the content, using standard language codes), but the idea of coding information about computer language is good. 关于lang属性, lang="php"是不正确的(根据HTML规范, lang属性使用标准语言代码指定内容的人类语言),但是对计算机语言的信息进行编码的想法很不错。 It may help styling and scripting later. 稍后可能有助于样式和脚本。 HTML5 drafts mention that such information can be coded using a class name that starts with language- , eg class="language-php"' (or, when combined with another class name, class="language-php pre"'. HTML5草案提到,可以使用以language-开头的类名对此类信息进行编码,例如class="language-php"' (or, when combined with another class name, class =“ language-php pre”')。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM