Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP将字符串限制为x,然后剪切li并将其存储在一个值中供以后使用_Php_String_Concatenation_Limit - Fatal编程技术网

PHP将字符串限制为x,然后剪切li并将其存储在一个值中供以后使用

PHP将字符串限制为x,然后剪切li并将其存储在一个值中供以后使用,php,string,concatenation,limit,Php,String,Concatenation,Limit,我有这个html作为描述 <p>Intro Description here</p> <ul> <li>Magnetic numbers are perfect for steel safety scoreboards.</li> <li>Numbers are white and are 20-mil thick and 2" high. </li> <li>Sold in full package

我有这个html作为描述

<p>Intro Description here</p>
<ul>
<li>Magnetic numbers are perfect for steel safety scoreboards.</li>
<li>Numbers are white and are 20-mil thick and 2" high. </li>
<li>Sold in full packages of 10 numbers (0-9).</li>
</ul>
此处介绍说明

  • 磁性数字是钢制安全记分牌的完美选择
  • 数字为白色,厚20密耳,高2英寸
  • 以10个号码(0-9)的完整包装出售

我希望有90个字符的限制,所以在这个特定的例子中,它以第二个
  • 结尾。我如何剪切和存储(它在一个变量中供以后使用,同一页)第二个和第三个
  • 完全忽略了90个字符的限制。我的意思是,如果第90个字符属于第二个
  • ,那么
  • 及其内容将被切断并存储在一个变量中。

    您应该尝试获得如下ul元素:

    $u = $dom->getElementsByTagName("ul"); 
    
    获取内部html

    $str = $u->saveHTML() ;
    
    完整代码(假设您只需要标签内的零件)


    不容易。最简单的方法是逐节运行dom树,并计算字符数,直到达到限制。这样您就知道要使用哪个元素进行切片,并且可以使用核弹攻击以下任何元素。我编辑了存储值,您可能会对此感到困惑。感谢您的快速回复忽略
  • 的部分如何?我假设你不想计算html标记,比如
  • ?$dom->plainText,我测试了你的代码,即使我回显$string,我仍然得到纯文本我不确定你到底在问什么,你提到你不想要html标记(纯文本)对吗?很抱歉造成混淆。我编辑了这个问题供你参考。谢谢!
    $html = "Your html string"
    $dom = new domDocument; 
    $dom->loadHTML($html);
    $u = $dom->getElementsByTagName("ul"); 
    $str = $u->saveHTML() ;
    $string = substr($str,0,90)  //getting only 90 chars