mを使用するphp function複数行の文字列から不要な空白をすべて削除する関数を作成したいとします。複数行の文字列にregexを使用して空白と空白を整えます
それは働いていない正規表現は、最後にスペースを削除いずれかになります。
// Always trim at the end. Warning: this seems to be the costlier
// operation, perhaps because looking ahead is harder?
$patterns[] = ['/ +$/m', ''];
はテキストエリアから次の文字列を考える:
first line... abc //<-- blank space here
second is here... def //<-- blank space here
//<-- blank space here
fourth line... hi there //<-- blank space here
sith is here.... //<-- blank space here
を先頭に空白があると各行の終わりに単語の間に複数の行が加えられます。私は、関数を実行した後
:
$functions->trimWhitespace($description, ['blankLines' => false]);
これは私が得るものです:
first line... abc //<-- blank space here
second is here... def //<-- blank space here
//<-- no bank space here
fourth line... hi there //<-- blank space here
sith is here....//<-- no blank space here
なぜそれが唯一の最後の行から末尾のスペースを削除して
?
使用する改行の種類についてわからない場合は、[''/ + \ r?$/m''](https://eval.in/730414)を試してください。 –
それはPHPなので、 ''/ \ h + $/um''を使わないでください。 –
@bobblebubble Yay!それが欠けていたものです。私は現在Linuxに入っていますが、WindowsとMacで動作するかどうかはわかりません。解決策を回答に入れてください。ありがとう。 –