Bir görüntünün kaynağını ayıklamak için sözdizimi regex

5 Cevap php

Orada Ahoy!

Ben cadı sözdizimi ben bir görüntünün kaynağını ayıklamak mümkün kullanabilirsiniz ama sadece web adresi değil src= tırnak ne olmalıdır "tahmin" olamaz?

İşte kod benim parçasıdır:

function get_all_images_src() {
    $content = get_the_content();
    preg_match_all('|src="(.*?)"|i', $content, $matches, PREG_SET_ORDER);
    foreach($matches as $path) {
    	echo $path[0];
    }
}

Bunu kullandığınızda bu baskılı var:

src="http://project.bechade.fr/wp-content/uploads/2009/09/mer-300x225.jpg"

Ve ben sadece bu almak istiyoruz:

http://project.bechade.fr/wp-content/uploads/2009/09/mer-300x225.jpg

Herhangi bir fikir?

Yardımlarınız için teşekkürler.

5 Cevap

Thanks Ithcy for his right answer. I guess I've been too long to respond because he deleted it, I just don't know where his answer's gone...

Yani burada ben posta yoluyla aldık biridir:

'|src="(.*?)"|i' makes no sense as a regex. try '|src="([^"]+)"|i' instead. (Which still isn't the most robust solution but is better than what you've got.)

Also, what everyone else said. You want $path[1], NOT $path[0]. You're already extracting all the src attributes into $matches[]. That has nothing to do with $path[0]. If you're not getting all of the src attributes in the text, there is a problem somewhere else in your code.

One more thing - you should use a real HTML parser for this, because img tags are not the only tags with src attributes. If you're using this code on raw HTML source, it's going to match not just but tags, etc.

- Ithcy

Ben o Bart bir HTML çözümleyici (2 cevap) kullanılarak dahil yapmak için bana her şeyi yaptım.

Bu bir cazibe gibi çalışır! Eğer dostum teşekkürler ...

Bir Sorunuzun cevabı, ancak html ayrıştırma zaman, uygun bir html parser kullanarak düşünmüyoruz tam:

foreach($html->find('img') as $element) {
  echo $element->src . '<br />';
}

Bkz: http://simplehtmldom.sourceforge.net/

$path[1] yerine $path[0] bölgesinin

echo $path[1];

$ Yol [0] eşleşen tam dizedir. $ Yol [1] ilk gruptur.

Eğer doğru dizesi olacaktır olsun dizideki Ayraç ve daha sonra ikinci öğe olarak "kullanarak dize patlayabilir:

$ Dizi = ('"', $ full_src) patlayabilir;

$ Bit_you_want = $ dizi [1];

Orijinal işlevi reworking, olurdu:

function get_all_images_src() {    
$content = get_the_content();    
preg_match_all('|src="(.*?)"|i', $content, $matches, PREG_SET_ORDER);    
foreach($matches as $path) {   
$src = explode('"', $path);     
echo $src[1];    
}
}