2012-11-15 20 views
0

可能重複:
Robust, Mature HTML Parser for PHP爆炸STRING的第一句第一個圖像

我試圖抓住一個字符串,並在第一圖像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" /> 

回答

0

如果你使用的PREG_SPLIT_DELIM_CAPTURE你需要提供preg_split使用正則表達式模式中捕獲。

在當前的模式:

/<img/ 

還有就是mothing捕捉到,這就是爲什麼你看到它刪除(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解析器

0

獲得的第一句話是非常簡單的。您只需使用strpossubstr的混合物,如下所示。至於獲得第一個圖片標籤,您可以使用preg_match表達式來實現。

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

1)第一句

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

2)IMG

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