の実質ニューラインに私は<br>
タグでテキストを持っていると私は新しい行としてMySQLデータベースに保存したいです。 HTMLタグではありません。例えばPHPのstr_replace <br>、 rを n個のMySQL
:
$string = 'some text with<br>tags here.'
、私はこのようにMySQLの中にそれを保存したい:この目的のためにどのような権利str_replace
some text with tags here
?ありがとうございました。
の実質ニューラインに私は<br>
タグでテキストを持っていると私は新しい行としてMySQLデータベースに保存したいです。 HTMLタグではありません。例えばPHPのstr_replace <br>、 rを n個のMySQL
:
$string = 'some text with<br>tags here.'
、私はこのようにMySQLの中にそれを保存したい:この目的のためにどのような権利str_replace
some text with tags here
?ありがとうございました。
ここでこれを試してください。これにより、<br>
がすべて\r\n
に置き換えられます。
$string = 'some text with<br>tags here.';
str_replace("<br>","\r\n",$string);
echo $string;
出力:あなたが提案するよう
some text with
tags here.
あなたは、str_replace
を使用することができます。
$string = 'some text with<br>tags here.';
$string = str_replace('<br>', "\r\n", $string);
あなた<br>
タグはまた、<br />
または<br/>
を閉鎖することができるならば、それはpreg_replace
を使用して検討する価値があるかもしれ、が。
$string = 'some text with<br>tags here.';
$string = preg_replace('/<br(\s+\/)?>/', "\r\n", $string);
あなたはhtmlentities
を使用することができます - 文字にHTMLエンティティを変換するエンティティに、すべてのHTML文字を変換し、html_entity_decode
$string = 'some text with<br>tags here'
$a = htmlentities($string);
$b = html_entity_decode($a);
echo $a; // some text with<br>tags here
echo $b; // some text with<br>tags here
試してみてください。
function safe($value){
return mysql_real_escape_string($value);
}
をmysql_real_escape_stringの
PHPには、新しい行をnl2br()
というbrに変換する関数がすでにあります。しかし、その逆は真ではありません。代わりに、あなたは次のように独自の関数を作成することができます。
function br2nl($string)
{
$breaks = array("<br />","<br>","<br/>");
return str_ireplace($breaks, "\r\n", $string);
}
を次に、あなたがそれを使用したいとき、次のようにそれを呼び出す:
$original_string = 'some text with<br>tags here.';
$good_string = br2nl($original_string);
は言及する価値が三つあります
PHP_EOL
を使用することを好むかもしれません。これはあなたがどんなシステムにいても正しい改行文字を与えます。<br />
などの非標準HTMLやその他のバリエーションは考慮されていません。これらのバリエーションを考慮する必要がある場合は、preg_replace()
機能を使用する必要があります。それがあれば、可能なすべてのバリエーションを打ち倒すことができますが、それでもすべてを説明することはできません。たとえば、<br id="mybreak">
と他の多くの属性と空白の組み合わせを考えてみましょう。