【问题标题】:How to count characters on a single html line with PHP如何使用 PHP 计算单个 html 行中的字符数
【发布时间】:2015-02-11 02:53:51
【问题描述】:

如何使用 PHP 计算单个 HTML 行(不包括标签但包括​​空格)上的字符数?

在我知道单个 HTML 行(包括空格)上的最大字符数后,我需要计算链接的字符(如果有的话)和之前|之后的字符。

所以有了这些信息:

  • 最大行字符。
  • 如果链接:| 之前的字符之后和本身。

HTML 示例:

    <p>Line one Line one  Line one  Line one Line one Line Line one  Line one 
    Line two Line two Line two Line two <a href="#link">LINK</a> Line two
    Line three Line three  Line three Line three Line three Line three</p> 

PHP:

读取第一行,计算最大字符数,如果链接,则

如果链接:计数链接字符加上之前|后。 用

创建数组
"before linked text, amount of characters"
"linked text, amount of characters"
"after linked text, amount of characters"

任何帮助或见解都会很棒!谢谢你的时间。

【问题讨论】:

    标签: php html count character


    【解决方案1】:

    使用 strlen()

    例子:

    $str = 'hello world';
    $length = strlen($str);
    echo $length; //returns 11
    

    View Documentation Here

    【讨论】:

    • 感谢您的回答(我知道使用 strlen),但我如何计算前后以及链接本身?
    • 链接是静态的还是会改变?
    • 链接会改变,大多数时候甚至不会有一个,但它可能会出现在行中的任何位置。
    • 另一个问题是不知道该行在html中的开始或结束位置
    猜你喜欢
    • 1970-01-01
    • 2020-07-10
    • 2013-07-04
    • 1970-01-01
    • 2016-02-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    相关资源
    最近更新 更多