PHP/PHP PHP - html 태그 추출기 - 오늘의 명언 개요 웹개발을 할때 svg 아이콘이 필요하면 유튜브에서 사용하는 svg 태그를 가져다 사용하고 있는데 유튜브에 존재하는 모든 svg를 미리 분류해두고 싶어 php를 사용해 유튜브 페이지의 전체 html에서 svg 태그만 추출하기 위해 php로 태그 추출기를 만들어보았다. 코드 <?php // 추출할 태그명을 변수로 정의 $tagName = 'div'; // 원하는 태그명으로 변경 (예: 'div', 'p' 등) // HTML 파일 읽기 $html = file_get_contents('input.html'); // 동적 태그명으로 패턴 생성 $pattern = "/<{$tagName}.*?>(.*?)<\/{$tagName}>/is"; preg_match_all($pattern, $html, $matches); // 결과 처리 $output = implode("\n", $matches[0]); // 파일로 저장 file_put_contents('output.html', $output); echo "태그 추출이 완료되었습니다."; 사용 예시 input.html <html> <body> <div class="div1"> <span></span> </div> <div class="div2"> <p></p> </div> <p></p> <span></span> <div class="div3"></div> </body> </html> 위 html을 php 파일과 같은 경로에 input.html로 저장하고 php 파일의 $tagName 변수를 추출하고 싶은 태그(예시에서는 div)로 작성한 뒤 터미널에 php index.php 명령어를 사용하면 태그 추출이 완료되었습니다. 라는 메시지와 함께 아래의 내용이 담긴 output.html 파일이 저장된다. output.html <div class="div1"> <span></span> </div> <div class="div2"> <p></p> </div> <div class="div3"></div> 반응형 공유하기 URL 복사카카오톡 공유페이스북 공유엑스 공유 게시글 관리 구독하기Aleph Kim의 IT 블로그 Contents 개요 코드 사용예시 input.html output.html 당신이 좋아할만한 콘텐츠 PHP - Docker-Compose 환경 PHP 프로젝트 빌드 시 Composer 설치 2025.03.11 PHP - 내장 함수만으로 .env 파일 사용하기 2025.03.07 PHP - include, include_once, require, require_once 차이 2025.03.05 PHP - homebrew 특정 PHP 버전 설치 (zsh: command not found: php 에러) 2024.12.24 댓글 0 + 이전 댓글 더보기