2013-08-05 5 views
0

저는 PHP에 익숙하지 않아 도움이 필요합니다. 이 변수에서 이미지와 내용을 나누어야합니다. 이미지와 설명이 있습니다.변수에서 src와 내용 가져 오기

$content = "<a href="/pin/211106301253428599/"> 
<img src="http://media-cache-ak0.pinimg.com/192x/8d/97/f5/8d97f59de2c2d5d8d83fa61f1f4ad7a5.jpg"></a> 
No matter where or why you travel, there's always something wonderfully new to be found! <a href="https://stackoverflow.com/search?q=quote" class="pintag" title="#quote search Pinterest">#quote</a>"; 

나는 이것이 단순하지만 뭔가를 알고 있습니다. 나는 다른 곳에서 링크가있는 변수와 내용의 이미지가 필요하다.

고마워.

+0

당신은에 포함 된 따옴표를 이스케이프 할 필요가 문자열. – Barmar

+1

HTML 데이터에서 정보를 얻으려면 DOM 구문 분석 라이브러리를 사용해야합니다. – Barmar

+0

큰 따옴표를 신중하게 사용해야하며 작은 따옴표를 사용할 수도 있습니다. –

답변

0

@Barmar에 따르면 DOM 파싱 라이브러리를 사용해야합니다.

당신은 찾을 수 있습니다 매우 유용이 : http://simplehtmldom.sourceforge.net

그것의 사용은 jQuery를 구문 분석과 유사합니다, 당신은 단지 그것을 (아주 좋은 예 =>http://simplehtmldom.sourceforge.net/manual.htm)

를 사용하는 방법을 알고 문서를 읽을 필요 예 :

$content = "<a href="/pin/211106301253428599/"> 
<img src="http://media-cache-ak0.pinimg.com/192x/8d/97/f5/8d97f59de2c2d5d8d83fa61f1f4ad7a5.jpg"></a> 
No matter where or why you travel, there's always something wonderfully new to be found! <a href="https://stackoverflow.com/search?q=quote" class="pintag" title="#quote search Pinterest">#quote</a>"; 

$html = str_get_html($content); 

$image = $html->find('img'); 
$links = $html->find('a'); 

그리고 당신은, 당신이 링크를 $ 이미지에 원하는 $ 무엇을해야합니다 : D

+0

SimpleHtmlDom은 다소 느린 코드베이스를 가진 느린 메모리 돼지입니다. 다음 중 하나를 고려하십시오. http://stackoverflow.com/questions/3577641/how-to-parse-and-process-html-with-php/3577662#3577662 – Gordon

+0

흥미 롭습니다. SimpleHtmlDom을 사용했기 때문에 SimpleHtmlDom에 답했고 메모리 문제 (작은 페이지를 파싱 한 것뿐이었습니다.)로 작동했기 때문에, 그리고 다른 것에 대해 몰랐기 때문에 작동했습니다. Gordon에게 더 많은 것을 가르쳐 주셔서 감사합니다. XD – Chococroc