Skip to content

berry.utils.HTMLHelper

Nikos Siatras edited this page Dec 13, 2025 · 13 revisions

HTMLHelper.GetHTMLTags

Description

public function GetHTMLTags(string $tag, string $html): array

This function returns an array with HTML objects of the given tag type contained in the given html string.

Parameters

  • $tag is the the tag to find
  • $html is the html string to search for tag objects

Example

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...">
)

HTMLHelper.URLFriendly

Description

public function URLFriendly(string $string): string

Converts a string to a URL friendy format

Parameters

  • $string is the string to convert to URL friendly

Example

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

HTMLHelper.MakeHTMLSafe

Description

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.

Parameters

  • $str is the string to be escaped for HTML output
  • $encoding (optional) is the character encoding to use (default: 'UTF-8')

Example

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:

&lt;script&gt;alert(&quot;XSS&quot;)&lt;/script&gt; &amp; &quot;quotes&quot;

Clone this wiki locally