HTMLタグで終わらないすべての行に追加しようとしています<br/>
が、うまくいきません。
私はこれまでのところこれを持っていますが、(PHPでは)まったく一致しないようです。
$message=preg_replace("/^(.*[^>])([\n\r])$/","\${1}<br/>\${2}",$message);
これを適切に機能させる方法についてのアイデアはありますか?
http://phpfiddle.org/main/code/259-vvpを参照してください。
<?php
//
$message0 = "You are OK.
<p>You are good,</p>
You are the universe.
<strong>Go to school</strong>
This is the end.
";
//
if(preg_match("/^WIN/i", PHP_OS))
{
$message = preg_replace('#(?<!\w>)[\r]$#m', '<br />', $message0);
}
else
{
$message = preg_replace('#(?<!\w>)$#m', '<br />', $message0);
}
echo "<textarea style=\"width: 700px; height: 90px;\">";
echo($message);
echo "</textarea>";
//
?>
与えます:
You are OK.<br />
<p>You are good,</p>
You are the universe.<br />
<strong>Go to school</strong>
This is the end.<br /><br />
< br /> のような HTML タグで終わっていない場合は、< br /> を追加します: < /p>、</strong>、...
説明:
(?<!\w>): negative lookbehind, if a newline character is not preceded
by a partial html close tag, \w word character + closing >, like a>, 1> for h1>, ...
[\r\n]*$: end by any newline character or not.
m: modifier for multiline mode.
これを使用できます:
$message = preg_replace('~(?<![\h>])\h*\R~', '<br/>', $message);
どこ:
`\h` is for horizontal white spaces (space and tab)
`\R` is for newline
(?<!..) is a negative lookbehind (not preceded by ..)