3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

LaravelにASCII制御文字を削除する処理を追加

Posted at

Laravel(5.1)にASCII制御文字を削除する処理がない?ので追加してみました。

これで、リスクエスト値全てに制御文字削除の処理が実行されます。

\App\Http\Middleware\RemoveInvisibleCharacters.php
namespace App\Http\Middleware;
use Closure;

class RemoveInvisibleCharacters
{
    public function handle($request, Closure $next)
    {
        $input = $request->all();

        $input = $this->removeInvisible($input);

        $request->replace($input);
        return $next($request);
    }

    private function removeInvisible($input)
    {
        if(is_array($input)){
            foreach($input as $key => $val){
                $input[$key] = $this->removeInvisible($val);
            }
        }else{
            return preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S', '', $input);
        }
        return $input;
    }
}
\App\Http\Kernel.php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    protected $middleware = [
        \App\Http\Middleware\RemoveInvisibleCharacters::class,
    ];

    protected $routeMiddleware = [];
}
3
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?