wp_html_split()WP 4.2.4

Parses the given text (in the form of HTML code) into HTML tags and comments, returning an array of values: tags and their text.

The function may be useful when you need to replace/process a string that is inside an HTML tag or vice versa process the HTML tags themselves. For example, in WP this function is used for processing shortcodes inside HTML tags, in tag attributes, see code do_shortcodes_in_html_tags().

1 time — 0.000167 sec (fast) | 50000 times — 0.14 sec (very fast) | PHP 7.1.11, WP 4.9.8

No Hooks.

Returns

String[]. Parsed text into tags.

Usage

wp_html_split( $input );
$input(string) (required)
String (HTML code) that needs to be parsed into parts.

Examples

0

#1 An example of how the function parses HTML code

$html = '<h1>Title text</h1>
<p>The text in the paragraph.</p>
<p class="func_note" title="text for attribute">Paragraph with attributes.</p>
<h5>Subtitle</h5>
<br />
Just text without tags.
<!-- HTML comment-->
<dl>
	<dt>DT text <em>nested in DT tag</em></dt>
	<dd> DD text.</dd>
</dl>';

$splited_html = wp_html_split( $html );

We get an array like this:

Array
(
	[0] => 
	[1] => <h1>
	[2] => Title text
	[3] => </h1>
	[4] => 

	[5] => <p>
	[6] => The text in the paragraph.
	[7] => </p>
	[8] => 

	[9] => <p class="func_note" title="text for attribute">
	[10] => Paragraph with attributes.
	[11] => </p>
	[12] => 

	[13] => <h5>
	[14] => Subtitle
	[15] => </h5>
	[16] => 

	[17] => <br />
	[18] => 
Just text without tags.

	[19] => <!-- HTML comment -->
	[20] => 

	[21] => <dl>
	[22] => 

	[23] => <dt>
	[24] => DT text 
	[25] => <em>
	[26] => nested in DT tag
	[27] => </em>
	[28] => 
	[29] => </dt>
	[30] => 

	[31] => <dd>
	[32] => DD text.
	[33] => </dd>
	[34] => 

	[35] => </dl>
	[36] => 
)
0

#2 Collect all IMG tags from the passed content

This is a demo example, in general, it's easier to collect IMG tags with a regular.

$content = 'Text <img src="/foo.jpg" /> <p>paragraph</p> <img src="/bar.jpg" />';

$textarr = wp_html_split( $content );
$imgs = [];

foreach( $textarr as $element ){

	// this is not a tag, skip it (this check is just an example)
	if( '' == trim( $element ) || '<' !== $element[0] ){
		continue;
	}

	if( substr( $element, 1, 3 ) === 'img' ){
		$imgs[] = $element;
	}
}

/* $imgs
Array(
	[0] => <img src="/foo.jpg" />
	[1] => <img src="/bar.jpg" />
)
*/

Changelog

Since 4.2.4 Introduced.

wp_html_split() code WP 6.8.3

function wp_html_split( $input ) {
	return preg_split( get_html_split_regex(), $input, -1, PREG_SPLIT_DELIM_CAPTURE );
}