WP_oEmbed::_strip_newlines() WordPress Method

The WP_oEmbed::_strip_newlines() method is used to remove newlines from a string. This is useful for removing extra whitespace from a string that is going to be used in an HTML attribute.

WP_oEmbed::_strip_newlines( string $html, object $data, string $url ) #

Strips any new lines from the HTML.


Parameters

$html

(string)(Required)Existing HTML.

$data

(object)(Required)Data object from WP_oEmbed::data2html()

$url

(string)(Required)The original URL passed to oEmbed.


Top ↑

Return

(string) Possibly modified $html


Top ↑

Source

File: wp-includes/class-wp-oembed.php

	public function _strip_newlines( $html, $data, $url ) {
		if ( false === strpos( $html, "\n" ) ) {
			return $html;
		}

		$count     = 1;
		$found     = array();
		$token     = '__PRE__';
		$search    = array( "\t", "\n", "\r", ' ' );
		$replace   = array( '__TAB__', '__NL__', '__CR__', '__SPACE__' );
		$tokenized = str_replace( $search, $replace, $html );

		preg_match_all( '#(<pre[^>]*>.+?</pre>)#i', $tokenized, $matches, PREG_SET_ORDER );
		foreach ( $matches as $i => $match ) {
			$tag_html  = str_replace( $replace, $search, $match[0] );
			$tag_token = $token . $i;

			$found[ $tag_token ] = $tag_html;
			$html                = str_replace( $tag_html, $tag_token, $html, $count );
		}

		$replaced = str_replace( $replace, $search, $html );
		$stripped = str_replace( array( "\r\n", "\n" ), '', $replaced );
		$pre      = array_values( $found );
		$tokens   = array_keys( $found );

		return str_replace( $tokens, $pre, $stripped );
	}

Top ↑

Changelog

Changelog
VersionDescription
3.0.0
2.9.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.