Html lib
Utility for managing HTML document structure, including title, meta tags, CSS/JS assets, and content formatting.
Access the library via the shared lib instance:
$html = $this->lib->html; // instance of html_lib
set_title()
Set the title of the HTML document.
Parameters: string $title
Usage Example:
$this->lib->html->set_title('My Page Title');
get_title()
Get the current title of the HTML document.
Returns: string
Usage Example:
$title = $this->lib->html->get_title();
echo $title; // Outputs: "My Page Title"
print_title()
Print the HTML title tag with the document title.
Usage Example:
$this->lib->html->print_title();
// Output: <title>My Page Title</title>
attach_file()
Attach JS or CSS files to the document.
Parameters: string $file_url
Returns: bool — true on success, triggers warning on invalid file type
Usage Example:
// Attach CSS file
$this->lib->html->attach_file('/assets/css/style.css');
// Attach JS file
$this->lib->html->attach_file('/assets/js/script.js');
// Invalid file type (triggers warning)
$this->lib->html->attach_file('/assets/font.woff'); // Warning: Trying to attach invalid file format
Note: Only .js and .css files are allowed. Duplicate files are ignored.
get_attached_files()
Get array of attached files by type.
Parameters: string $type — 'css' or 'js'
Returns: array|bool — Array of file URLs or false for invalid type
Usage Example:
$css_files = $this->lib->html->get_attached_files('css');
$js_files = $this->lib->html->get_attached_files('js');
$invalid = $this->lib->html->get_attached_files('html'); // false
print_attached_js_files()
Print HTML script tags for all attached JavaScript files.
Returns: bool — true if files were printed, false if no JS files attached
Usage Example:
$this->lib->html->print_attached_js_files();
// Output:
// <script type="text/javascript" src="/assets/js/script.js"></script>
// <script type="text/javascript" src="/assets/js/app.js"></script>
print_attached_css_files()
Print HTML link tags for all attached CSS files.
Returns: bool — true if files were printed, false if no CSS files attached
Usage Example:
$this->lib->html->print_attached_css_files();
// Output:
// <link rel="stylesheet" type="text/css" href="/assets/css/style.css" />
// <link rel="stylesheet" type="text/css" href="/assets/css/theme.css" />
print_attached_files()
Print attached CSS/JS files. If type is specified, prints only that type.
Parameters: string $type — 'css', 'js', or empty string for both
Usage Example:
// Print only CSS files
$this->lib->html->print_attached_files('css');
// Print only JS files
$this->lib->html->print_attached_files('js');
// Print both CSS and JS files (default)
$this->lib->html->print_attached_files();
set_meta()
Set meta content for the HTML document.
Parameters:
string $item— Meta name (e.g., 'description', 'keywords')string $value— Meta content value
Usage Example:
$this->lib->html->set_meta('description', 'This is my awesome website');
$this->lib->html->set_meta('keywords', 'php, framework, web development');
Note: Double quotes in the value are automatically converted to single quotes.
set_head_tag()
Set custom head tag content to be included in the HTML head section.
Parameters: string $head_tag
Usage Example:
$this->lib->html->set_head_tag('<meta charset="UTF-8">');
$this->lib->html->set_head_tag('<meta name="viewport" content="width=device-width, initial-scale=1.0">');
get_meta()
Get meta content from the HTML document.
Parameters: string $item — Optional. If null, returns all meta data
Returns: string|array — Single meta value or all meta data as array
Usage Example:
// Get specific meta value
$description = $this->lib->html->get_meta('description');
// Get all meta data
$all_meta = $this->lib->html->get_meta();
// Returns: ['keywords' => '...', 'description' => '...']
print_meta()
Print meta content with HTML tags.
Parameters: string $item — Optional. If null, prints all meta tags
Usage Example:
// Print specific meta tag
$this->lib->html->print_meta('description');
// Output: <meta name="description" content="This is my awesome website" />
// Print all meta tags
$this->lib->html->print_meta();
// Output:
// <meta name="keywords" content="php, framework, web development" />
// <meta name="description" content="This is my awesome website" />
print_head_tags()
Print all custom head tags added via set_head_tag().
Usage Example:
$this->lib->html->print_head_tags();
// Output:
// <meta charset="UTF-8">
// <meta name="viewport" content="width=device-width, initial-scale=1.0">
make_keywords()
Generate meta keywords from text content.
Parameters:
string $string— Source text to extract keywords fromint $keywords_count— Maximum number of keywords (default: 12)
Returns: string|bool — Keywords string or false on failure
Behavior:
- Removes HTML tags and special characters
- Filters words by length (5-15 characters)
- Counts word frequency and returns most common words
- Returns comma-separated keywords
Usage Example:
$text = 'PHP is a popular server-side scripting language designed for web development.';
$keywords = $this->lib->html->make_keywords($text, 5);
echo $keywords; // "server, scripting, popular, designed, development"
make_description()
Generate meta description from text content.
Parameters:
string $string— Source textint $count— Maximum character count (default: 300)
Returns: string|bool — Truncated description or false on empty input
Behavior:
- Removes HTML tags and special formatting
- Truncates at word boundary near the specified count
- Returns complete text if shorter than count
Usage Example:
$long_text = 'This is a very long description that needs to be truncated to fit within meta description length limits...';
$description = $this->lib->html->make_description($long_text, 100);
echo $description; // "This is a very long description that needs to be truncated to fit within meta"
format()
Format data for HTML output with optional encoding and replacements.
Parameters:
string $data— Data to formatbool $encode— Whether to encode HTML entities (default:false)array $rep_arr— Optional replacement pairs
Returns: string — Formatted HTML content
Usage Example:
// Basic formatting
$text = "Hello\nWorld";
$formatted = $this->lib->html->format($text);
// Output: "Hello<br />World"
// With HTML encoding
$html = "<script>alert('xss')</script>";
$safe = $this->lib->html->format($html, true);
// Output: "<script>alert('xss')</script>"
// With custom replacements
$content = "Hello {name}!";
$formatted = $this->lib->html->format($content, false, ['{name}' => 'John']);
// Output: "Hello John!"
json_to_html()
Convert JSON array to HTML div elements.
Parameters: string $json — JSON string
Returns: string — HTML representation
Behavior:
- Converts JSON to PHP array
- Creates div elements with class 'mr_1' for each value
- Uses array values as div content
Usage Example:
$json_data = '{"item1": "Apple", "item2": "Banana", "item3": "Orange"}';
$html = $this->lib->html->json_to_html($json_data);
echo $html;
// Output:
// <div class='mr_1'>Apple</div>
// <div class='mr_1'>Banana</div>
// <div class='mr_1'>Orange</div>
Complete Usage Example
// Initialize and set document properties
$this->lib->html->set_title('Product Catalog');
$this->lib->html->set_meta('description', 'Browse our complete product catalog');
$this->lib->html->set_meta('keywords', 'products, catalog, shopping');
// Add custom head tags
$this->lib->html->set_head_tag('<meta charset="UTF-8">');
$this->lib->html->set_head_tag('<meta name="viewport" content="width=device-width, initial-scale=1.0">');
// Attach CSS and JS files
$this->lib->html->attach_file('/assets/css/bootstrap.css');
$this->lib->html->attach_file('/assets/css/custom.css');
$this->lib->html->attach_file('/assets/js/jquery.js');
$this->lib->html->attach_file('/assets/js/app.js');
// In your HTML head section
<!DOCTYPE html>
<html>
<head>
<?php
$this->lib->html->print_title();
$this->lib->html->print_meta();
$this->lib->html->print_head_tags();
$this->lib->html->print_attached_files('css');
?>
</head>
<body>
<!-- Your content here -->
<?php $this->lib->html->print_attached_files('js'); ?>
</body>
</html>
Tips
- Call
attach_file()before printing to ensure files are included - Use
make_keywords()andmake_description()to auto-generate SEO meta content - Always encode user-provided content using
format($data, true)for XSS protection - Duplicate file attachments are automatically ignored, making it safe to call
attach_file()multiple times