在PHP中将HTML重新处理为单行字符串的最佳方法是什么?

在PHP中将HTML重新处理为单行字符串的最佳方法是什么?

问题描述:

I'm working on a WordPress plugin that exports posts and associated data into a tab-seperated text format. It's almost finished, but I'm struggling to find the best method to re-process HTML into single strings.

I'm using a combination of preg_replace and htmlentities, but it's getting a little messy.

I'm sure there must be a preferred method for doing this, so I thought I would check with you? I can't find anything suitable on Google.

我正在使用WordPress插件将帖子和相关数据导出为制表符分隔的文本格式。 它几乎已经完成了,但是我很难找到将HTML重新处理成单个字符串的最佳方法。 p>

我正在使用preg_replace和htmlentities的组合,但它正在变得有点 凌乱。 p>

我确信必须有一个首选的方法,所以我想我会和你一起检查? 我在Google上找不到合适的内容。 p> div>

There's no single sure fire way but some simple regex would do the trick:

// remove convert newlines and multiple spaces into 1 space
$html = preg_replace("/\s+|
+|/", ' ', $html);

Have you considered base64 encoding?

$foo = base64_encode($data);