ホームページ >バックエンド開発 >PHPチュートリアル >laravel' s string()メソッドを使用したストリップ操作を合理化します
Laravelのrequest->string()
メソッドは、弦操作に対する合理化されたアプローチを提供します。入力データをストリング可能なオブジェクトに変換し、効率的な変換をチェーンする流fluentメソッドを可能にします。
// Basic transformation $name = $request->string('name') ->trim() ->title() ->limit(50); // Input: $request->input('name') = ' jANE mARY smith '; // Output: 'Jane Mary Smith'プロファイルデータの消毒における実用的なアプリケーション:
<?php namespace App\Http\Controllers; use App\Models\Profile; use Illuminate\Http\Request; class ProfileController extends Controller { public function update(Request $request, Profile $profile) { $profile->update([ 'display_name' => $request->string('name') ->trim() ->title() ->limit(50) ->toString(), 'username' => $request->string('username') ->trim() ->lower() ->replaceMatches('/[^a-z0-9_-]/', '') ->limit(20) ->toString(), 'bio' => $request->string('bio') ->trim() ->stripTags() ->limit(160) ->toString(), 'website' => $request->string('website') ->trim() ->lower() ->replace(['http://', 'https://'], '') ->before('/') ->toString() ]); return response()->json([ 'message' => 'Profile updated successfully', 'profile' => $profile->fresh() ]); } }メソッドは、チェーンメソッド呼び出しを介して入力の消毒と変換を簡素化することにより、コードの読みやすさと保守性を向上させ、文字列操作のクリーナーとより効率的にします。
以上がlaravel&#039; s string()メソッドを使用したストリップ操作を合理化しますの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。