Magento2前端開發:使用PHP TIDY來補齊或移除不完整的HTML TAG

Magento2 後台的wisiwyg編輯器有防呆的機制,會自動修正使用者輸入的html,可以避免產生不完整的html tag。但有時候我們需要匯入一些資料,或是同步一些外部資料進來,這些匯入或是同步來的資料就有機會夾帶一些不完整的html tag,進而造成網頁的大破版,所以就算後台有再堅強的防呆機制,都沒辦法防止。因為他是不通過後台的!
使用 apt search php.*tidy 來搜尋php tidy 的版本,再從搜尋的結果中挑選適合的版本出來安裝。
此處範例用的php版本是 7.2,所以就要安裝php7.2-tidy
sudo apt-get install php7.2-tidy
tidy的基本用法
<?php
$html = '<p>If you are looking for HTML beautifier (a tool to indent HTML output produced by your script), <a href="#">Tidy extension might not be the right tool</h1></div> for the job.';
// Specify configuration
$config = array(
           'show-body-only' =>true,
           'indent'         => false,
           'wrap'           => 0
         );
// Tidy
$tidy = new tidy;
$tidy->parseString($html, $config, 'utf8');
$tidy->cleanRepair();
// Output
echo $tidy;
?>
輸入的結果會變成底下的樣子:
<p>If you are looking for HTML beautifier (a tool to indent HTML output produced by your script), <a href="#">Tidy extension might not be the right tool for the job.</a></p>
以上就是本次的分享,想收到更多Magento相關資訊,請訂閱我們的電子報,也別忘了追蹤歐斯瑞臉書粉絲團和Instagram唷!
想學習更多Magento設定嗎?請見:Magento教學導覽
 
	 
			 
			 
			 
			 
			 
			
我要留言