2

Does anybody know how can I replace HTML string with spaces?

Because if I have something like this

<div>word1</div><div>word2</div>

when I use strip_tags(), the result is

word1word2

and I need them to be separated by space in order to be processed the way they should be by my script.

4

3 回答 3

0

这将正常工作

$str = preg_replace('#<[^>]+>#', ' ', '<h1>Foo</h1>bar');

之后使用类似的东西

$str = preg_replace('/\s\s+/', ' ', $str);
于 2013-02-13T19:45:53.420 回答
0

你可以使用这个:

preg_replace('/<[^>]*>/', ' ', $input);

但是你的文本中最好不要有任何杂散<字符。

还:

preg_replace('/ {2,}/', ' ', $input);

会将多个空间折叠成一个空间。

于 2013-02-13T19:46:53.187 回答
0

这很容易,也可以理解。

<?php

$txt = "<div>word1</div><div>word2</div>";

$find = array("<div>", "</div>");
$replace = array(" ", " ");
echo str_replace($find, $replace, $txt);
于 2013-02-13T19:48:20.807 回答