Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Форматирование xml #67

Open
Septdir opened this issue Dec 8, 2020 · 0 comments
Open

Форматирование xml #67

Septdir opened this issue Dec 8, 2020 · 0 comments
Labels
На будущее Предожение функции

Comments

@Septdir
Copy link
Member

Septdir commented Dec 8, 2020

Добавить форматирование иходного кода xml файла

	protected function formatXmlString($xml)
	{
		$xml     = preg_replace('/(>)(<)(\/*)/', "$1\n$2$3", $xml);
		$token   = strtok($xml, "\n");
		$result  = '';
		$pad     = 0;
		$matches = array();
		while ($token !== false) :
			if (preg_match('/.+<\/\w[^>]*>$/', $token, $matches)) :
				$indent = 0;
			elseif (preg_match('/^<\/\w/', $token, $matches)) :
				$pad--;
				$indent = 0;
			elseif (preg_match('/^<\w[^>]*[^\/]>.*$/', $token, $matches)) :
				$indent = 1;
			else :
				$indent = 0;
			endif;
			$line   = str_pad($token, strlen($token) + $pad, '', STR_PAD_LEFT);
			$result .= $line . "\n";
			$token  = strtok("\n");
			$pad    += $indent;
		endwhile;

		return $result;
	}
@Septdir Septdir added the На будущее Предожение функции label Dec 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
На будущее Предожение функции
Projects
None yet
Development

No branches or pull requests

1 participant