PHP 코드관련
img 주소값에서 이미지만 가져오는 정규표현식 특정디렉터리만 가져오는 방법
땀모
2019. 7. 16. 18:11
이미지 주소만 가져오는 정규표현식
http://pcb.test/photos/shares/test/Hydrangeas.jpg
여기서 test라는 디렉터리를 가지고오는 방법(test는 항상변하는 값)
원본이미지말고 썸네일 이미지를 가지고 올수 있다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | $contents = "<img src='http://pcb.test/photos/shares/test/Hydrangeas.jpg' alt='' width='500' />"; preg_match_all("/<img[^>]*src=[\"']?([^>\"']+)[\"']?[^>]*>/i", stripslashes($contents), $matches); preg_match_all("/<img[^>]*src=[\"']?([^>\"']+)[\"']?[^>]*>/i", stripslashes($contents), $val); //이미지주소를 /구분으로 배열로 담는다. $var_arr = explode('/', $val[1][0]); //배열 5번째 index의 값을 꺼내온다. echo $var_arr[5]; echo "<br>"; // 이미지 echo $matches[0][0]; echo "<br>"; //echo $url = str_replace('/shares/', '/shares/thumbs/', $matches[0][0]); //주소값 http://pcb.test/photos/shares/test/Hydrangeas.jpg echo '원본주소: '.$matches[1][0]."<br>"; echo '추가: '.$url = str_replace('/shares/', '/shares/', $matches[1][0])."<br>"; echo '추가2: '.$url = str_replace('/test/','/'.$var_arr[5].'/thumbs/', $url)."<br>"; //http://pcb.test/photos/shares/thumbs/test/Hydrangeas.jpg //echo $url = str_replace('/shares/', '/shares/thumbs/', $matches[0][0]) //주소값 http://pcb.test/photos/shares/test/thumbs/Hydrangeas.jpg //echo $matches[1][0]; | cs |