regex 정규식 질문 드립니다
본문
!시작!
<img src="https://www.naver.com/1.jpg" data-src="/data/daumeditor10/231115/thumbnail3/46321717000154390.jpeg" class="txc-image" width="760" style="""" / name="target_resize_image[]" onclick="org_win(this);" style="cursor:pointer;" >
!끝!
타사이트에서 예를 들어 위와 같은 내용을 가져오려하는데
src="https://www.naver.com/1.jpg" data-
이 부분을 제외하고
<img src="/data/daumeditor10/231115/thumbnail3/46321717000154390.jpeg" class="txc-image" width="760" style="""" / name="target_resize_image[]" onclick="org_win(this);" style="cursor:pointer;" >
이런식으로 가져올순 없나요?
어떠한 정규식을 짜야하는지 모르겠습니다
답변 3
안녕하세요.
원하시는 내용이 정확히 맞는지 모르겠지만 참고를 해보시겠어요~
<?php
$html = '!시작!<img src="https://www.naver.com/1.jpg" data-src="/data/daumeditor10/231115/thumbnail3/46321717000154390.jpeg" class="txc-image" width="760" style="""" / name="target_resize_image[]" onclick="org_win(this);" style="cursor:pointer;" >!끝!';
$pattern = '/<img[^>]*\ssrc=["\']([^"\']*)["\'][^>]*>/';
preg_match($pattern, $html, $matches);
if (isset($matches[1])) {
$imageUrl = $matches[1];
echo $imageUrl;
} else {
echo "이미지 URL을 찾을 수 없습니다.";
}
?>
다 균일한 형식이라면
img 부분만 따로 빼놓고 explode를 src="https://www.naver.com/1.jpg" data- 기준으로한다음
배열에서 빼고 처리하는 방법이있겠네요
$newtxt= preg_replace("/src.*data-/", '',$text);