Using WordPress ‘get_html_split_regex()’ PHP function

The get_html_split_regex WordPress PHP function retrieves the regular expression for an HTML element.

Usage

$regex = get_html_split_regex();

Parameters

This function does not have any parameters.

More information

See WordPress Developer Resources: get_html_split_regex

Examples

Splitting HTML content into an array

This code snippet splits an HTML content string into an array of elements.

$content = "<p>Hello, world!</p><div>Another element</div>";
$regex = get_html_split_regex();
$html_elements = preg_split($regex, $content, -1, PREG_SPLIT_DELIM_CAPTURE);
print_r($html_elements);

Counting the number of HTML elements in a string

This code snippet counts the number of HTML elements in a given string.

$html_string = "<p>First paragraph</p><div>Second element</div>";
$regex = get_html_split_regex();
$count = preg_match_all($regex, $html_string);
echo "Number of HTML elements: " . $count;

Removing all HTML tags from a string

This code snippet removes all HTML tags from a given string.

$html_string = "<p>Hello</p><h1>World</h1>";
$regex = get_html_split_regex();
$clean_string = preg_replace($regex, '', $html_string);
echo $clean_string;

Replacing all HTML tags with a custom string

This code snippet replaces all HTML tags in a given string with a custom string.

$html_string = "<h1>Title</h1><p>Paragraph</p>";
$regex = get_html_split_regex();
$replaced_string = preg_replace($regex, '[TAG]', $html_string);
echo $replaced_string;

Extracting all HTML tags from a string

This code snippet extracts all HTML tags from a given string and stores them in an array.

$html_string = "<h1>Heading</h1><p>Paragraph</p>";
$regex = get_html_split_regex();
preg_match_all($regex, $html_string, $matches, PREG_PATTERN_ORDER);
$tags = array_filter($matches[0], function($element) {
  return !ctype_space($element);
});
print_r($tags);