2

<div>给定一大块在和中很好地显示数据的 HTML <table>,如何在保留最初在单个单元格和 div 中找到的文本的同时删除所有 HTML/CSS 标记,现在只用换行符分隔?

此处显示的当前尝试将输出一个长的连续段落,而不是在 div 或表格形式时保持分隔。

原始 HTML: http: //pastebin.com/63N3Kg16

输出:

John Smith | SomeName Realty | (xxx) 939-4835 Allston St, Cambridge, MA Very spacious under renovation with SST/Granite, porch, minutes to MIT, redline, Nov/1 4BR/1BA Apartment $3,400/month Bedrooms 4 Bathrooms 1 full, 0 partial Sq Footage Unspecified Parking None Pet Policy No pets Deposit $0 DESCRIPTION Triple decker building secondfloor apt aprox 2000 sqf with large bedrooms, kitchen, pantry, porch, d/w, all woodfloor and ZTilded in the kitchen, new bath. utilities extra,Nov/1 see additional photos below Contact info: Payman Ahmadifar Bayside Realty (xxx) 939-4835 Posted: Sep 24, 2012, 6:55am PDT

PHP

nl2br(trim(strip_tags($html)));

预期产出

<br>带有或换行符、无<div><table>HTML 标记的纯文本。基本上是为了使文本更具可读性,保持原始的间距/分隔结构,但除了<br>.

John Smith | SomeName Realty | (xxx) 939-4835 

Allston St, Cambridge, MA 

Very spacious under renovation with SST/Granite, porch, minutes to MIT, redline, Nov/1 

4BR/1BA Apartment $3,400/month 

Bedrooms 4 
Bathrooms 1 full, 0 partial 
Sq Footage Unspecified 
Parking None 
Pet Policy No pets 
Deposit $0 

DESCRIPTION 
Triple decker building secondfloor apt aprox 2000 sqf with large bedrooms, kitchen, pantry, porch, d/w, all woodfloor and ZTilded in the kitchen, new bath. utilities extra,Nov/1 see additional photos below 

Contact info: Payman Ahmadifar Bayside Realty (xxx) 939-4835 
Posted: Sep 24, 2012, 6:55am PDT
4

1 回答 1

1

你可以玩一些字符串操作

尝试

$string = strip_tags($html);
$string = str_replace(chr(32).chr(32).chr(32),"*****",$string);
$newString = array_map(function($var){ return  trim(preg_replace('!\s+!', ' ',$var)); },explode("*****",$string));
print(implode("\n", $newString));

观看现场演示

于 2012-10-07T18:18:42.633 回答