cnblogs/dcrenl/C#HTML与UBB(纯文本)之间的转换.html
2024-09-24 12:43:01 +08:00

44 lines
6.0 KiB
HTML

<div id="sina_keyword_ad_area2" class="articalContent ">
<div>private string HtmlToUBB(string _Html)</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>{</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>_Html = Regex.Replace(_Html,&quot;&lt;br[^&gt;]*&gt;&quot;,&quot;\n&quot;);</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>_Html = Regex.Replace(_Html,@&quot;&lt;p[^&gt;\/]*\/&gt;&quot;,&quot;\n&quot;);</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>_Html = Regex.Replace(_Html,&quot;\\son[\\w]{3,16}\\s?=\\s*([\'\&quot;]).&#43;?\\1&quot;, &quot;&quot;);</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>_Html = Regex.Replace(_Html,&quot;&lt;hr[^&gt;]*&gt;&quot;,&quot;[hr]&quot;);</div>
<div><br>
</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>_Html = Regex.Replace(_Html,&quot;&lt;(\\/)?blockquote([^&gt;]*)&gt;&quot;,&quot;[$1blockquote]&quot;);</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>_Html = Regex.Replace(_Html,&quot;&lt;img[^&gt;]*smile=\&quot;(\\d&#43;)\&quot;[^&gt;]*&gt;&quot;,&quot;'[s:$1]&quot;);</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>_Html = Regex.Replace(_Html,&quot;&lt;img[^&gt;]*src=[\'\&quot;\\s]*([^\\s\'\&quot;]&#43;)[^&gt;]*&gt;&quot;,&quot;&quot;);</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>_Html = Regex.Replace(_Html,&quot;&lt;a[^&gt;]*href=[\'\&quot;\\s]*([^\\s\'\&quot;]*)[^&gt;]*&gt;(.&#43;?)&lt;\\/a&gt;&quot;,&quot;[url=$1]$2[/url]&quot;);</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>_Html = Regex.Replace(_Html,&quot;&lt;b&gt;(.&#43;?)&lt;/b&gt;&quot;,@&quot;\[b\]$1\[/b\]&quot;);</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>_Html = Regex.Replace(_Html,&quot;&lt;[^&gt;]*?&gt;&quot;, &quot;&quot;);</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>_Html = Regex.Replace(_Html,&quot;&amp;amp;&quot;, &quot;&amp;&quot;);</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>_Html = Regex.Replace(_Html,&quot;&amp;nbsp;&quot;, &quot; &quot;);</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>_Html = Regex.Replace(_Html,&quot;&amp;lt;&quot;, &quot;&lt;&quot;);</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>_Html = Regex.Replace(_Html,&quot;&amp;gt;&quot;, &quot;&gt;&quot;);</div>
<div><br>
</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>return _Html;</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>}</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>public string UBBToHtml(stringcontent) &nbsp;<wbr>//ubb转html</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>{</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>content = Regex.Replace(content, @&quot;\r\n&quot;,&quot;&lt;br/&gt;&quot;);</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>content = Regex.Replace(content, &quot; &quot;,&quot;&amp;nbsp;&quot;);</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>content = Regex.Replace(content,@&quot;\[b\](.&#43;?)\[/b\]&quot;,&quot;&lt;b&gt;$1&lt;/b&gt;&quot;);</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>content = Regex.Replace(content,@&quot;\[i\](.&#43;?)\[/i\]&quot;,&quot;&lt;i&gt;$1&lt;/i&gt;&quot;);</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>content = Regex.Replace(content,@&quot;\[u\](.&#43;?)\[/u\]&quot;,&quot;&lt;u&gt;$1&lt;/u&gt;&quot;);</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>content = Regex.Replace(content,@&quot;\[p\](.&#43;?)\[/p\]&quot;, &quot;&lt;pclass='load'&gt;$1&lt;/p&gt;&quot;);</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>content = Regex.Replace(content,@&quot;\[align=left\](.&#43;?)\[/align\]&quot;,&quot;&lt;align='left'&gt;$1&lt;/align&gt;&quot;);</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>content = Regex.Replace(content,@&quot;\[align=center\](.&#43;?)\[/align\]&quot;,&quot;&lt;align='center'&gt;$1&lt;/align&gt;&quot;);</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>content = Regex.Replace(content,@&quot;\[align=right\](.&#43;?)\[/align\]&quot;,&quot;&lt;align='right'&gt;$1&lt;/align&gt;&quot;);</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>content = Regex.Replace(content,@&quot;\[url=(?&lt;url&gt;.&#43;?)]\[/url]&quot;,&quot;&lt;a href='${url}'target=_blank&gt;${url}&lt;/a&gt;&quot;);</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>content = Regex.Replace(content,@&quot;\[url=(?&lt;url&gt;.&#43;?)](?&lt;name&gt;.&#43;?)\[/url]&quot;,&quot;&lt;a href='${url}'target=_blank&gt;${name}&lt;/a&gt;&quot;);</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>content = Regex.Replace(content,@&quot;\[quote](?&lt;text&gt;.&#43;?)\[/quote]&quot;,&quot;&lt;divclass=quote&gt;${text}&lt;/div&gt;&quot;);</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>content = Regex.Replace(content,@&quot;\[img](?&lt;img&gt;.&#43;?)\[/img]&quot;,&quot;&lt;img src='${img}' alt=''/&gt;&quot;);</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>return content;</div>
<div>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>}</div>
</div>