0

多くの '\n' を含むコメントを含むデータベースがあります '\n' (ビュー側) からすべてのコメントを消去する関数を作成します。それは正常に動作しますが、まだ \n を使用している一部のユーザーがいて、私の機能はそれをきれいにすることができません。奇妙なことに、DB からコメントをコピーして自分のサイトに貼り付けると、機能が正常に動作し、'\n' が消去されます。DB で自分のコメントを編集し、過去のユーザーのコメントを編集すると、関数が機能しませんでした。どうすればいいのかわかりません。選択クエリで「mysql replace」を使用しようとしましたが、うまくいきませんでした。phpMyAdminでselect replaceを使用すると機能するという別の奇妙なこと。私を助けてください..

私の機能:

function cleanComments($text){

    return stripslashes(str_replace("\\n", ". ",htmlspecialchars($text)));
} 

私のSQLクエリ:

SELECT REPLACE(comment_text, '\\n', '.'),date...

DBでコメントを変更せずにコメントから\n(文字列)をきれいにする方法を知っている人はいますか?

4

2 に答える 2

2

you must use \n ,not \\n

function cleanComments($text){

    return stripslashes(str_replace("\n", ". ",htmlspecialchars($text)));
}

this is because "\n" means new line and "\\n" means "\n" string;
example :

echo "\n";

result : a new line

echo "\\n";

result : \n

于 2013-08-18T13:07:33.357 に答える
1

On Windows, a newline is \r\n. Since most of your users are probably on Windows, this would explain why just removing \n is insufficient.

Now, are these actual newlines you're trying to remove, or literal "backslash-n"? If it's an actual newline, then you should do something like:

str_replace(["\n","\r"],"",$text);

If it's a literal, then double up those backslashes.

于 2013-08-18T13:07:39.530 に答える