Skip to content

berry.utils.HTMLHelper

Nikos Siatras edited this page Oct 1, 2022 · 12 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
Clone this wiki locally