Last active
May 9, 2016 13:27
-
-
Save igoralves1/bc3fb16742ec055b5066682e154c47de to your computer and use it in GitHub Desktop.
Clear HTML string from white spaces and new line character. It returns clean html string to be used inside JSON, AJAX etc.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Eu tenho a seguinte string que representa um código html gerado dinamicamente por php. | |
//Preciso filtrar novas linhas e espaços em brancos para ser formatado corretamente num string JSON para | |
//Ser enviado através de AJAX. | |
<ul class="list-group sidebar-nav-v1 margin-bottom-40" id="menuHomeUserPrivate"> | |
<li class="list-group-item active"> | |
<a id="to_ProfileOverall" class="privateMenuLinkJS"><i class="fa fa-bar-chart-o"></i> Overall</a> | |
</li> | |
<li class="list-group-item list-toggle"> | |
<a data-toggle="collapse" data-parent="#menuHomeUserPrivate" href="#collapse-MoneyManage" ><i class="fa fa-money"></i> Invoice</a> | |
<ul id="collapse-MoneyManage" class="collapse"> | |
<li><a id="to_MoneyManagerFaturamentoInsert" class="privateMenuLinkJS"><i class="fa fa-level-down"></i> Big Invoice </a></li> | |
<li><a id="to_MoneyManagerFaturamentoGerir" class="privateMenuLinkJS"><i class="fa fa-cogs"></i> Big big big | |
Invoice 2 </a></li> | |
</ul> | |
</li> | |
</ul> | |
//O objetivo é transformar em uma string de uma só linha, filtrar espaços em branco e new lines. | |
<ul class="list-group sidebar-nav-v1 margin-bottom-40" id="menuHomeUserPrivate"><li class="list-group-item active"><a id="to_ProfileOverall" class="privateMenuLinkJS"><i class="fa fa-bar-chart-o"></i>Overall</a></li><li class="list-group-item list-toggle"><a data-toggle="collapse" data-parent="#menuHomeUserPrivate" href="#collapse-MoneyManage" ><i class="fa fa-money"></i> Invoice</a><ul id="collapse-MoneyManage" class="collapse"><li><a id="to_MoneyManagerFaturamentoInsert" class="privateMenuLinkJS"><i class="fa fa-level-down"></i>Big Invoice</a></li><li><a id="to_MoneyManagerFaturamentoGerir" class="privateMenuLinkJS"><i class="fa fa-cogs"></i>Big big big Invoice 2</a></li></ul></li></ul> | |
//Então essa é a abordagem: | |
//Gerar o html dinamicamente com o heredoc http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc | |
$htlmString=<<<EOF | |
<ul class="list-group sidebar-nav-v1 margin-bottom-40" id="menuHomeUserPrivate"> | |
<li class="list-group-item active"> | |
<a id="to_ProfileOverall" class="privateMenuLinkJS"><i class="fa fa-bar-chart-o"></i> Overall</a> | |
</li> | |
<li class="list-group-item list-toggle"> | |
<a data-toggle="collapse" data-parent="#menuHomeUserPrivate" href="#collapse-MoneyManage" ><i class="fa fa-money"></i> Invoice</a> | |
<ul id="collapse-MoneyManage" class="collapse"> | |
<li><a id="to_MoneyManagerFaturamentoInsert" class="privateMenuLinkJS"><i class="fa fa-level-down"></i> Big Invoice </a></li> | |
<li><a id="to_MoneyManagerFaturamentoGerir" class="privateMenuLinkJS"><i class="fa fa-cogs"></i> Big big big | |
Invoice 2 </a></li> | |
</ul> | |
</li> | |
</ul> | |
EOF; | |
$re = "/(?:\\s*([<>])\\s*|(\\s)\\s*)/im"; | |
$subst = "$1$2"; | |
$htmlDone = preg_replace($re, $subst, $str); | |
Fonte:http://stackoverflow.com/questions/35732977/regex-how-to-remove-white-spaces-and-new-lines-in-html-code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment