-
Notifications
You must be signed in to change notification settings - Fork 0
berry.utils.HTMLHelper
Nikos Siatras edited this page Dec 13, 2025
·
13 revisions
public function GetHTMLTags(string $tag, string $html): arrayThis function returns an array with HTML objects of the given tag type contained in the given html string.
- $tag is the the tag to find
- $html is the html string to search for tag objects
The following example finds and prints all img tags within an html string.
require_once(__DIR__ . "/berry/utils.php"); // Include berry utils package
$htmlString = '<span>Hello this is an image <img src="myImage1.png" alt="Image"> and here is an other one <img src="myImage2.png" alt="An other one..."></span>';
$htmlHelper = new HTMLHelper();
// Get all <img> tags from $htmlString
$images = $htmlHelper->GetHTMLTags("img", $htmlString);
print_r($images);The above example will output:
Array
(
[0] => <img src="myImage1.png" alt="Image">
[1] => <img src="myImage2.png" alt="An other one...">
)
public function URLFriendly(string $string): stringConverts a string to a URL friendy format
- $string is the string to convert to URL friendly
require_once(__DIR__ . "/berry/utils.php"); // Include berry utils package
// Initialize an HTMLHelper
$htmlHelper = new HTMLHelper();
$string = "PHP-Berry framework for PHP backend applications";
// Convert $string to url friendly and print it
print $htmlHelper->URLFriendly($string);The above example will output:
php-berry-framework-for-php-backend-applications
public function MakeHTMLSafe($str, $encoding = 'UTF-8')Returns an HTML-safe version of the given string. Converts special HTML characters (such as <, >, &, and ") to their HTML entities, making the string safe for display in an HTML context and helping prevent XSS attacks.
- $str is the string to be escaped for HTML output
- $encoding (optional) is the character encoding to use (default: 'UTF-8')
require_once(__DIR__ . "/berry/utils.php"); // Include berry utils package
// Initialize an HTMLHelper
$htmlHelper = new HTMLHelper();
$string = '<script>alert("XSS")</script> & "quotes"';
// Escape $string for HTML and print it
print $htmlHelper->MakeHTMLSafe($string);The above example will output:
<script>alert("XSS")</script> & "quotes"