LoginSignup
0
2

More than 5 years have passed since last update.

Phalcon 3 改行コードをとにかく1バイトとする処理 formで使える!

Posted at

改行を1バイトで計算させる

TextAreaで入力した文字で改行コードがWindowsだと\r\nとなってしまって2バイト扱いとなります。
そのほかMac,Linuxだと1バイトなんですが改行コードが違います。
そのへんをすべて吸収させる処理を考えてみました。

CRキャリッジリターンとLFラインフィード

改行コードはCRだったりLFだったり、CRLFだったりシステムによって異なります。

改行コード
LF UNIXやUnix系のシステム。Linux、macOS
CR+LF Windows
CR 昔のMac OS

PhalconのformsTextAreaに入力された文字を自動で変換する方法があります。config/services.phpに以下のように追加します。

config/services.php
$di->set('filter', function () {
    $filter = new Filter();
    $filter->add('convertCRLF', function ($arg) {
        $to = "\n";
        return preg_replace("/\r\n|\r|\n/", "\n", $arg);
    });
    return $filter;
}, true);

実際に使う場合は以下の通り。

cofms/myform.php
      $msg = new TextArea('msg');
      $msg->addValidators([
      $msg->addFilter('striptags');
      $msg->addFilter("convertCRLF"); // <- ここね
      $this->add($msg);
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2