wp_html_split()WP 4.2.4

Separate HTML elements and comments from the text.

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

No Hooks.

Return

String[]. Array of the formatted text.

Usage

wp_html_split( $input );
$input(string) (required)
The text which has to be formatted.

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.4.3

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