Html entity encoder/decoder

Detail refer to http://andrewu.co.uk/clj/entityencode/.

ExpandedBlockStart.gif ContractedBlock.gif function  TextToEntities(strPlainText, blnPartialEncodeOnly)  dot.gif {
InBlock.gif    
var strPartial  = [];
InBlock.gif    
var strFull     = [];
InBlock.gif    
var intP        = 0;
InBlock.gif    
var intF        = 0;
InBlock.gif    
var objPartialRegExp = (new RegExp).compile("[\\w\\s]");
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
for (var intI=0; intI<strPlainText.length; ++intI) dot.gif{
InBlock.gif        
var strChar = strPlainText.charAt(intI);
InBlock.gif        
var intChar = strChar.charCodeAt(0);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (isNaN(intChar)) dot.gif{
InBlock.gif            
// IF CHAR FAILED TO DECODE, LEAVE AS CHAR
InBlock.gif
            strPartial.push(strFull.push(strChar));
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
else dot.gif{
InBlock.gif            
var strEntity = "&#" + intChar + ";";
InBlock.gif            strFull.push(strEntity);
InBlock.gif            
// IF CHAR WAS [a-zA-Z0-9_ \t] LEAVE AS CHAR, ELSE REPLACE WITH ENTITY
InBlock.gif
            strPartial.push(objPartialRegExp.test(strChar) ? strChar : strEntity);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
return (blnPartialEncodeOnly ? strPartial.join("") : strFull.join(""));
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
function  EntitiesToText(strEncodedText)  dot.gif {
InBlock.gif    
var strData     = String(strEncodedText);
InBlock.gif    
var objRegExp   = (new RegExp).compile("&#(\\d+);""ig");
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//* FOR EACH MATCH TO ANY ENTITY, REPLACE THAT
ExpandedSubBlockEnd.gif    ENTITY GLOBALLY WITH ITS SINGLE CHAR EQUIVALENT 
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
while(strData.match(objRegExp)) dot.gif{
InBlock.gif        
var strCharMatch    = RegExp.$1;
InBlock.gif        
var objRegExpMatch  = new RegExp("&#" + strCharMatch + ";""ig");
InBlock.gif        strData 
= strData.replace(objRegExpMatch, String.fromCharCode(strCharMatch));
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return strData;
ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/fengzhimei/archive/2005/11/25/284572.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值