2017-11-27 15 views
-3

JPEG/PNG 이미지에 대한 링크를 찾으려고 시도했습니다. 내가 확인을 작동 파일 확장자 checkm없이 링크 검색이 :JPG/PNG 링크의 경우 preg_match_all

preg_match_all('/<a.+href=[\'"]([^\'"]+).[\'"].*><img/i', $text, $matches); 

하지만 지금은 PNG/JPG에 대한 필터를 추가하려고 :(당신이 나를 도와 드릴까요 .. DOMDocument를이 해결

+4

정규식을 사용하여 HTML을 구문 분석하는 것은 [나쁜 생각]입니다 (https://stackoverflow.com/questions/590747/using-regular-expressions-to-parse-html-why-not). [ 이상한 결과] (https://stackoverflow.com/a/1732454/2370483) – Machavity

+2

''필터를 추가하려고합니다. '- 특별히 * * * 당신이 시도한 것은 무엇입니까?'(png | jpg)' 당신의 시도의 정규식 부분 어딘가에? (구문을 알지 않고 연구하지 않는 것은 두 가지 다른 점에 유의하십시오.) – mario

+0

mario, 예, 추가하려고했습니다. 하지만 $ matches에 확장자가없는 URL이 있습니다. – Rudomilov

답변

0

을?

$dom = new DOMDocument(); 
@$dom->loadHTML($post->post_content); 
$dom->preserveWhiteSpace = false; 

$images = $dom->getElementsByTagName('img'); 

foreach ($images as $image) { 
    if($image->parentNode->nodeName=='a') { 
     print $image->parentNode->getAttribute('href'); 
    } 
}