PHP 페이지의 HTML 출력 축소

John Wachira 2023년6월20일
  1. PHP 페이지의 HTML 출력 축소
  2. Apache에서 GZip 압축을 사용하여 PHP 페이지의 HTML 출력 축소
  3. ob_start() 함수를 콜백과 함께 사용하여 PHP 페이지의 HTML 출력 최소화
  4. HTML 축소기 플러그인을 사용하여 PHP 페이지의 HTML 출력 축소
PHP 페이지의 HTML 출력 축소

이 문서에서는 PHP 페이지에 대한 HTML 출력 축소에 대해 설명합니다.

PHP 페이지의 HTML 출력 축소

전반적인 웹 사이트 성능과 사용자 경험을 개선하기 위해 출력을 최소화합니다. 이 프로세스에는 웹 사이트 사용자를 위해 불필요한 파일을 제거하고 웹 사이트 페이지의 페이지 크기 및 로드 시간을 줄이는 작업이 포함됩니다.

또한 웹 사이트 사용자는 축소된 페이지를 통해 리소스 또는 데이터 사용량을 최소화할 수 있습니다. 축소 프로세스는 불필요한 세부 정보, 줄 바꿈, 주석 및 과도한 공백을 제거합니다.

프로세스의 단점은 코드 가독성이 떨어진다는 것입니다. 축소하면 파일 크기를 70%까지 줄일 수 있습니다.

HTML 출력을 수동 또는 자동으로 축소할 수 있습니다.

코드에서 공백을 복원할 수 있는 몇 가지 도구가 있습니다. 그러나 스크립트에 있는 주석에 대한 변경 사항을 되돌릴 수는 없습니다. 예를 살펴보겠습니다.

<html>

<head>

<!-- This content will show on the browser -->

<title>Title Page</title>

</head>

<body>

<!-- This is a comment. -->

<h1>Delftstack Tutorials!</h1>
</body>

</html>

위의 파일에는 많은 공백, 줄 바꿈 및 두 개의 주석이 있습니다. 파일을 축소하면 다음과 같을 것입니다.

<html><head><title>Title Page</title></head><body><h1>Delftstack Tutorials!</h1></body></html>

PHP 페이지에 대한 HTML 출력을 축소하는 다양한 방법을 살펴보겠습니다.

Apache에서 GZip 압축을 사용하여 PHP 페이지의 HTML 출력 축소

Apache에서 GZip 압축을 활성화하여 출력을 축소할 수 있습니다. 아래 단계를 따르십시오.

  1. Apache 구성 파일을 찾아 엽니다. 메모장을 사용하여 아래에서 편집할 수 있습니다.

    디렉터리는 시스템에 따라 다를 수 있지만 httpd.conf 파일을 열어야 합니다.

vim /etc/httpd/conf/httpd.conf
  1. 구성 파일에서 #을 추가하여 아래 행을 확인하십시오.

    LoadModule deflate_module modules/mod_deflate.so
    
  2. 아래 줄을 복사하여 구성 파일 끝에 붙여넣습니다.

    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/x-javascript
    
  3. Apache 서버를 다시 시작하십시오.

    sudo service httpd restart
    

ob_start() 함수를 콜백과 함께 사용하여 PHP 페이지의 HTML 출력 최소화

콜백과 함께 ob_start() 함수를 사용하여 태그, 주석 및 공백 시퀀스 앞뒤의 공백을 제거할 수 있습니다.

예제 코드를 살펴보겠습니다.

<?php
ob_start("minifier");
function minifier($code) {
    $search = array(
        // Remove whitespaces after tags
        '/\>[^\S ]+/s',
        // Remove whitespaces before tags
        '/[^\S ]+\</s',
        // Remove multiple whitespace sequences
        '/(\s)+/s',
        // Removes comments
        '/<!--(.|\s)*?-->/'
    );
    $replace = array('>', '<', '\\1');
    $code = preg_replace($search, $replace, $code);
    return $code;
}
?>
<!DOCTYPE html>
<html>
<head>
<!-- Page Title -->
<title>Sample Minifier</title>
</head>
<body>
<!-- page body -->
<h1>Delftstack Tutorials!</h1>
</body>
</html>
<?php
ob_end_flush();
?>

위의 코드를 실행한 후 아래와 같은 결과를 얻었습니다.

<!DOCTYPE html><html><head><title>Sample minifier</title></head><body><h1>Delftstack Tutorials!</h1></body></html>

HTML 축소기 플러그인을 사용하여 PHP 페이지의 HTML 출력 축소

우리는 클라이언트 측의 출력을 최적화하기 위해 서버 측의 HTML 축소기를 소스 코드로 사용합니다. 플러그인은 불필요한 공백, 줄 바꿈 및 주석을 제거합니다.

필요에 따라 최적화 옵션 풀에서 선택할 수 있습니다. 플러그인을 설정하려면 다음 단계를 따르세요.

  1. 아래 링크를 사용하여 컴퓨터에 HTML 축소기 파일을 다운로드합니다.

    https://www.terresquall.com/download/HTMLMinifier.php
    
  2. 아래 코드를 복사하여 PHP 파일에 붙여넣습니다.

    <?php
    // Import the HTMLMinifier
    require_once 'myfolder/HTMLMinifier.php';
    // HTML source to be minified
    $htmlpage = file_get_contents('./mypage.html');
    // Minified version of the page
    echo HTMLMinifier::process($htmlpage);
    ?>
    
  3. PHP 파일을 실행합니다.

작가: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

관련 문장 - PHP HTML