【问题标题】:Explode String Getting First Sentence First Image [duplicate]爆炸字符串获取第一句话第一个图像[重复]
【发布时间】:2012-11-04 13:57:51
【问题描述】:

可能重复:
Robust, Mature HTML Parser for PHP

我正在尝试抓取字符串的第一句话和第一个图像 html 实例。

$description = preg_split('/<img/', $item->description,null,PREG_SPLIT_DELIM_CAPTURE);

我能够返回一个数组,但它正在从它需要的值中删除&lt;img。我尝试过使用标志,但无法获得我正在寻找的需要包含分隔符本身的返回。我知道要抓住第一句话,我应该可以按句号或&amp;nbsp;

字符串:

<p>First sentence here comes.&nbsp; Second sentence here it is.&nbsp; One more sentence.&nbsp;&nbsp;</p> <img alt="amj" src="https://domain.com/images7.jpg" /> <img alt="Ea" src="http://domain.com/images3.jpg" /> <img alt="amj" src="https://domain.com/images7.jpg" /> <img alt="amj" src="https://domain.com/images7.jpg" />

【问题讨论】:

    标签: php html regex string


    【解决方案1】:

    获取第一句话非常简单。您只需要混合使用strpossubstr,如下所示。至于获取第一个图像标签,您可以使用preg_match 表达式来完成。

    $first_sentence = substr($item->description, 0, strpos($item->description, ))
    

    【讨论】:

      【解决方案2】:

      1) 第一句话

      echo substr($item->description, 0, strpos('.', $item->description));
      

      2) 图像

      preg_match('#<img[^>]*>#',$item->description , $img);
      echo $img[0];
      

      【讨论】:

        【解决方案3】:

        如果您使用PREG_SPLIT_DELIM_CAPTURE,您需要在与preg_split 一起使用的正则表达式模式中提供捕获。

        在你当前的模式中:

        /<img/
        

        有东西可以捕捉,这就是为什么你看到它被删除了 (Demo):

        Array
        (
            [0] => <p>First sentence here comes.&nbsp; Second sentence here it is.&nbsp; One more sentence.&nbsp;&nbsp;</p> 
            [1] =>  alt="amj" src="https://domain.com/images7.jpg" /> 
            [2] =>  alt="Ea" src="http://domain.com/images3.jpg" /> 
            [3] =>  alt="amj" src="https://domain.com/images7.jpg" /> 
            [4] =>  alt="amj" src="https://domain.com/images7.jpg" />
        )
        

        但是,如果您从中创建捕获,它将被捕获:

        /(<img)/
        

        结果(Demo):

        Array
        (
            [0] => <p>First sentence here comes.&nbsp; Second sentence here it is.&nbsp; One more sentence.&nbsp;&nbsp;</p> 
            [1] => <img
            [2] =>  alt="amj" src="https://domain.com/images7.jpg" /> 
            [3] => <img
            [4] =>  alt="Ea" src="http://domain.com/images3.jpg" /> 
            [5] => <img
            [6] =>  alt="amj" src="https://domain.com/images7.jpg" /> 
            [7] => <img
            [8] =>  alt="amj" src="https://domain.com/images7.jpg" />
        )
        

        如您所见,preg_split 完成了记录在案的工作,并将为每个捕获第一个捕获 supgroup 的每个捕获添加 另一个 拆分(它只会占用第一个)。然后,您可能需要将其扩展到完整标记,例如,在不同的其他类似 html-like-string-regex 的问题中已经概述了它(像往常一样受到正则表达式的限制,所以怪罪于您使用 preg_* 函数而不是 HTML如果遇到问题,解析器,而不是模式本身:

        /(<img [^>]*>)/
        

        结果(Demo):

        Array
        (
            [0] => <p>First sentence here comes.&nbsp; Second sentence here it is.&nbsp; One more sentence.&nbsp;&nbsp;</p> 
            [1] => <img alt="amj" src="https://domain.com/images7.jpg" />
            [2] =>  
            [3] => <img alt="Ea" src="http://domain.com/images3.jpg" />
            [4] =>  
            [5] => <img alt="amj" src="https://domain.com/images7.jpg" />
            [6] =>  
            [7] => <img alt="amj" src="https://domain.com/images7.jpg" />
            [8] => 
        )
        

        您可以使用标准的 HTML 解析器使您的代码更加稳定。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-02-15
          • 2018-07-30
          • 1970-01-01
          • 1970-01-01
          • 2013-12-15
          • 1970-01-01
          • 2017-05-31
          • 2011-10-29
          相关资源
          最近更新 更多