开发者

PRE tag to Plain Text

开发者 https://www.devze.com 2022-12-12 13:40 出处:网络
I am making a site where code will be displayed as plain text in a PRE tag (no highlighting, comments, no edit tags) and the I want them to be able to click a button and have the highlighted, commente

I am making a site where code will be displayed as plain text in a PRE tag (no highlighting, comments, no edit tags) and the I want them to be able to click a button and have the highlighted, commented and edit tag code to appear (also in a pre tag) .

Obviously I have to store the 开发者_JS百科highlighted HTML code somewhere but I was looking for a function like a html2text that will take every essence of the edited PRE tag and convert it to plain text with the plain PRE tag

For example something like this

<pre> 
<b>ON SELECT</b> 
{
   "<edit title="This will be the filename.">beep.wav</edit>" SOUND
}
</pre> 

to something like this

<pre> 
ON SELECT 
{
   beep.wav SOUND
}
</pre> 

Edit: also anything that would go in a pre tag that is non-html would'nt have < > tags. There would be > and <'s but not an inclosed < > tag. The scripting language doesnt use it. So that means really all I would need is something to extract any closed < > tags.


In Javascript, you could do with DOM functions + regex

<body>
<pre> 
<b>ON SELECT</b> 
{
   "<edit title="This will be the filename.">beep.wav</edit>" SOUND
}
</pre>

<textarea id="result" rows=10 cols=50></textarea>

<script>
    x=document.getElementsByTagName("pre");
    for(i in x){
        text=x[i].innerHTML.replace(/<.*?>/g,'');
        text=text.replace(/"/g,'');
        document.getElementById("result").value+="<pre>"+text+"</pre>";
    }
</script>
</body>

That you will give your expected results, adjust it depends on your need.

<pre> 
ON SELECT 
{
   beep.wav SOUND
}
</pre>

or in PHP, you could use some parsers for HTML

0

精彩评论

暂无评论...
验证码 换一张
取 消