1
是否可以用<br>
标记替换换行符,但忽略跟在</h1>
标记后面的换行符?用br替换换行符,但在h1之后忽略换行符PHP
因此,例如,我想改变这个块:
<h1>Test</h1> \n some test here \n and here
要这样:
<h1>Test</h1> some test here <br /> and here
是否可以用<br>
标记替换换行符,但忽略跟在</h1>
标记后面的换行符?用br替换换行符,但在h1之后忽略换行符PHP
因此,例如,我想改变这个块:
<h1>Test</h1> \n some test here \n and here
要这样:
<h1>Test</h1> some test here <br /> and here
$subject = "<h1>hithere</h1>\nbut this other\nnewline should be replaced.";
$new = preg_replace("/(?<!h1\>)\n/","<br/>",$subject);
echo $new;
// results in:
// <h1>hithere</h1>
// but this other<br/>newline should be replaced.
应该工作。这是说\ n不受H1立即preceeded>
使用字符串函数,而不是正则表达式,并假设</h1>
标签可以是上线(而不是只换行)之前的任何地方:
$lines=file($fn);
foreach ($lines as $line) {
if (stristr("$line", "</h1>") == FALSE) {
$line = str_replace("\n", "<br />", $line);
}
echo $line;
}
这为你的榜样,其结果是:
<h1>
测试</h1>
一些测试这里<br />
这里<br />
为什么你会在第一个地方有这样的代码?看来你的原始状况本身就是一个问题。 – newbie 2010-03-05 22:17:25