0

可能的重复:
用于 PHP 的强大、成熟的 HTML 解析器

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

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

我能够返回一个数组,但它正在<img从它的值中删除所需的值。我尝试过使用标志,但无法获得我正在寻找的需要包含分隔符本身的返回。我知道要抓住第一句话,我应该能够按句点或&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" />
4

3 回答 3

0

获取第一句话非常简单。您只需要使用 和 的混合物,strpos如下substr所示。至于获取第一个图像标签,您可以使用preg_match表达式来做到这一点。

$first_sentence = substr($item->description, 0, strpos($item->description, ))
于 2012-11-15T21:39:27.273 回答
0

1) 第一句话

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

2) 图像

preg_match('#<img[^>]*>#',$item->description , $img);
echo $img[0];
于 2012-11-15T21:44:33.067 回答
0

如果您使用,则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)/

结果(演示):

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 [^>]*>)/

结果(演示):

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 解析器,您可以使您的代码更加稳定。

于 2012-11-16T08:42:38.783 回答