progphil1337/php-html

使用PHP创建HTML代码

v1.0.2 2022-09-15 08:16 UTC

This package is auto-updated.

Last update: 2024-09-15 12:43:41 UTC


README

使用PHP创建有效的HTML代码

安装

使用composer安装

$ composer require progphil1337/php-html

兼容性

ProgPhil1337\HTML需要PHP 8.1(或更高版本)。

用法

基本示例

use ProgPhil1337\HTML\Attribute\Style;
use ProgPhil1337\HTML\Element;

$html = new Element('html');

// Create <head> with <title>
$head = new Element('head');
$title = new Element('title');
$title->innerText('Website Title');

$head->add($title);

$html->add($head);

// Create <body> with styled box
$body = new Element('body');
$body->add(new Style([
    'background-color' => '#ecf0f1',
    'width' => '600px',
    'margin' => '0 auto'
]));

$div = new Element('div');
$div->add(new Style([
    'background-color' => 'white',
    'margin-top' => '50px',
    'border-radius' => '3px',
    'padding' => '13px'
]));

$div->appendHTML('<h1>PHP-HTML</h1><br />');
$div->appendText('Create valid HTML code with PHP.');

$body->add($div);
$html->add($body);

echo $html;

输出以下HTML代码

<html>
<head>
<title>
Website Title
</title>
</head>
<body style="background-color:#ecf0f1;width:600px;margin:0 auto">
<div style="background-color:white;margin-top:50px;border-radius:3px;padding:13px">
<h1>PHP-HTML</h1><br />
Create valid HTML code with PHP.
</div>
</body>
</html>