public static function minify($expr)
	{
		$old     = $expr;
		$strings = [];

		// Trim the surrounding whitespace then temporarily remove literal strings
		$expr = preg_replace_callback(
			'/"[^"]*"|\'[^\']*\'/',
			function ($m) use (&$strings)
			{
				$uniqid = '(' . sha1(uniqid()) . ')';
				$strings[$uniqid] = $m[0];

				return $uniqid;
			},
			trim($expr)
		);

		if (preg_match('/[\'"]/', $expr))
		{
			throw new RuntimeException("Cannot parse XPath expression '" . $old . "'");
		}

		// Normalize whitespace to a single space
		$expr = preg_replace('/\\s+/', ' ', $expr);

		// Remove the space between a non-word character and a word character
		$expr = preg_replace('/([-a-z_0-9]) ([^-a-z_0-9])/i', '$1$2', $expr);
		$expr = preg_replace('/([^-a-z_0-9]) ([-a-z_0-9])/i', '$1$2', $expr);

		// Remove the space between two non-word characters as long as they're not two -
		$expr = preg_replace('/(?!- -)([^-a-z_0-9]) ([^-a-z_0-9])/i', '$1$2', $expr);

		// Remove the space between a - and a word character, as long as there's a space before -
		$expr = preg_replace('/ - ([a-z_0-9])/i', ' -$1', $expr);

		// Remove the spaces between a number and the div operator and the next token
		$expr = preg_replace('/((?:^|[ \\(])\\d+) div ?/', '$1div', $expr);

		// Remove the space between the div operator the next token
		$expr = preg_replace('/([^-a-z_0-9]div) (?=[$0-9@])/', '$1', $expr);

		// Restore the literals
		$expr = strtr($expr, $strings);

		return $expr;
	}
public static function minify($expr)
	{
		$old     = $expr;
		$strings = [];

		// Trim the surrounding whitespace then temporarily remove literal strings
		$expr = preg_replace_callback(
			'/"[^"]*"|\'[^\']*\'/',
			function ($m) use (&$strings)
			{
				$uniqid = '(' . sha1(uniqid()) . ')';
				$strings[$uniqid] = $m[0];

				return $uniqid;
			},
			trim($expr)
		);

		if (preg_match('/[\'"]/', $expr))
		{
			throw new RuntimeException("Cannot parse XPath expression '" . $old . "'");
		}

		// Normalize whitespace to a single space
		$expr = preg_replace('/\\s+/', ' ', $expr);

		// Remove the space between a non-word character and a word character
		$expr = preg_replace('/([-a-z_0-9]) ([^-a-z_0-9])/i', '$1$2', $expr);
		$expr = preg_replace('/([^-a-z_0-9]) ([-a-z_0-9])/i', '$1$2', $expr);

		// Remove the space between two non-word characters as long as they're not two -
		$expr = preg_replace('/(?!- -)([^-a-z_0-9]) ([^-a-z_0-9])/i', '$1$2', $expr);

		// Remove the space between a - and a word character, as long as there's a space before -
		$expr = preg_replace('/ - ([a-z_0-9])/i', ' -$1', $expr);

		// Remove the spaces between a number and the div operator and the next token
		$expr = preg_replace('/((?:^|[ \\(])\\d+) div ?/', '$1div', $expr);

		// Remove the space between the div operator the next token
		$expr = preg_replace('/([^-a-z_0-9]div) (?=[$0-9@])/', '$1', $expr);

		// Restore the literals
		$expr = strtr($expr, $strings);

		return $expr;
	}
public static function minify ( $ expr ) { $ old = $ expr ; $ strings = [ ] ; // Trim the surrounding whitespace then temporarily remove literal strings $ expr = preg_replace_callback ( '/"[^"]*"|\'[^\']*\'/' , function ( $ m ) use ( & $ strings ) { $ uniqid = '(' . sha1 ( uniqid ( ) ) . ')' ; $ strings [ $ uniqid ] = $ m [ 0 ] ; return $ uniqid ; } , trim ( $ expr ) ) ; if ( preg_match ( '/[\'"]/' , $ expr ) ) { throw new RuntimeException ( "Cannot parse XPath expression '" . $ old . "'" ) ; } // Normalize whitespace to a single space $ expr = preg_replace ( '/\\s+/' , ' ' , $ expr ) ; // Remove the space between a non-word character and a word character $ expr = preg_replace ( '/([-a-z_0-9]) ([^-a-z_0-9])/i' , '$1$2' , $ expr ) ; $ expr = preg_replace ( '/([^-a-z_0-9]) ([-a-z_0-9])/i' , '$1$2' , $ expr ) ; // Remove the space between two non-word characters as long as they're not two - $ expr = preg_replace ( '/(?!- -)([^-a-z_0-9]) ([^-a-z_0-9])/i' , '$1$2' , $ expr ) ; // Remove the space between a - and a word character, as long as there's a space before - $ expr = preg_replace ( '/ - ([a-z_0-9])/i' , ' -$1' , $ expr ) ; // Remove the spaces between a number and the div operator and the next token $ expr = preg_replace ( '/((?:^|[ \\(])\\d+) div ?/' , '$1div' , $ expr ) ; // Remove the space between the div operator the next token $ expr = preg_replace ( '/([^-a-z_0-9]div) (?=[$0-9@])/' , '$1' , $ expr ) ; // Restore the literals $ expr = strtr ( $ expr , $ strings ) ; return $ expr ; }
Remove extraneous space in a given XPath expression

@param  string $expr Original XPath expression
@return string       Minified XPath expression
Remove extraneous space in a given XPath expression