2016-07-30 27 views
-2

你好,我有这些孤立的HTML代码(如我输入):解析HTML表和格式化了的文本

案例1:

<td class="C" width="10%"> 
    <a href="URL1" onclick="ValDobleSubmit()"> 
    String1 
     <span style="color: blue; font-weight:bold;"> 
      String2 
     </span> 
    </a> 
</td> 
<td class="C" width="15%"> 
    String3 
</td> 
<td class="t" align="left" width="15%"> 
    String4 
</td> 
<td class="t" align="left" width="10%"> 
    String5 
</td> 

案例2

<td class="C" width="10%"> 
    <a href="URL1" onclick="ValDobleSubmit()"> 
    String1 
    </a> 
</td> 
<td class="C" width="15%"> 
    String3 
</td> 
<td class="t" align="left" width="15%"> 
    String4 
</td> 
<td class="t" align="left" width="10%"> 
    String5 
</td> 

我想用bash shell脚本或PHP(控制台)以便获得此输出:

  • 案例1:

String1String2 | URL1 | STRING3 |串,4 | STRING5

  • 案例2:

的String1 | URL1 | STRING3 |串,4 | STRING5

+0

请张贴一些你写的代码证明你努力解决这个问题。也看看是什么让[mcve] – sjsam

+0

对不起,我忘了提到我的努力。 我的输入是在一个file.txt中,所以我尝试了case1: cat file.txt | grep -v“<”| while read a;做echo $ a“|”;完成| sed':a; N; $!ba; s/\ n// g'| sed's/| * $ // g 我可以得到: String1 |字符串2 | STRING3 |串,4 | String5 – fermaf

+0

我有一个使用这个基本的解决方案,但问题是当我的输入文件与几个案件一起在同一个文件中。 – fermaf

回答

0
 $DOM = new DOMDocument; 
    $DOM->loadHTML($CaseSource); 
    $cells = $DOM->getElementsByTagName('td'); 
    foreach($cells as $cell){ 
$out[] = $cell->nodeValue; 
} 
+0

也可以使用strip_tags();转义html标签 –