0

in actionscript ,i didn't find a direct way to get the actually string's length of a html text.here's how i get things work

var myText:String = "<p>This is <b>some</b> content to <i>render</i> as <u>HTML</u> text.</p>"; 
myTextBox.htmlText = myText;
trace(myTextBox.length);

deal with large content of html text would be a performance problem.

is there a way i can get the length while i don't have to pass it to a text device?

4

1 回答 1

1

我看到了两种从 xml 中提取文本的方法:

  1. 最好的xHTML方法是将其解析为 XML 并提取所有文本节点
  2. 对于所有类型的文本,您可以尝试RegExp匹配不属于 HTML 标记的文本 ( http://regexr.com?363li )

    var s:String = "<p>This is <b>some</b> content to <i>render</i> as <u>HTML</u> text.</p>";
    
    //by TextField
    var tf:TextField = new TextField();
    tf.htmlText = s;
    trace(tf.text);
    trace(tf.length);
    
    //well-formed XML
    XML.ignoreWhitespace = false;
    var x:XML = new XML(s);
    
    var t:String = "";
    var list:XMLList = x..*;
    for each(var node:XML in list)
        if(node.nodeKind() == "text")
            t += node;
    
    trace(t);
    trace(t.length);
    
    //by RegExp (non wel formed XML)
    var match:Array = s.match(/(?<=^|>)[^><]+?(?=<|$)/gs);
    s = match.join("");
    trace(t);
    trace(t.length);
    

输出:

22528
21  ms
22528
35  ms
22528
20  ms

但是所有这些技术在性能上似乎几乎相等,正如您可以看到对于具有 22k 个字符的字符串,所有方法在 20-30 毫秒内以几乎相同的结果运行,但无论如何您都可以尝试这两种方法作为输入。

于 2013-08-26T11:32:03.223 回答