LaravelのdoesntContain
メソッドは、文字列に何かが欠落しているかどうかを確認するためのより直感的な方法を提供します。このメソッドは、既存のcontains
メソッドを補完し、ネガティブチェック用のクリーンな構文を提供します。
use Illuminate\Support\Str; // 基本用法 $text = "Welcome to Laravel"; $result = Str::doesntContain($text, 'PHP'); // true // 多重检查 $result = Str::doesntContain($text, ['PHP', 'Laravel']); // false
以下は、メッセージフィルタリングサービスを実装する実際の例です。
<?php namespace App\Services; use App\Models\Message; use Illuminate\Support\Str; class MessageFilter { protected array $sensitiveTerms = [ 'confidential', 'internal', 'classified' ]; public function isSafeForPublic(Message $message): bool { return Str::doesntContain( strtolower($message->content), $this->sensitiveTerms ); } public function processMessage(Message $message): array { if ($this->isSafeForPublic($message)) { $message->update(['status' => 'published']); return ['status' => 'success', 'message' => 'Message published']; } $message->update(['status' => 'review_required']); return ['status' => 'pending', 'message' => 'Content needs review']; } }
メソッドは、Laravelアプリケーションの文字列検証を簡素化し、特定のコンテンツがないことを確認するためのより直感的な構文を提供します。コンテンツ監査システム、入力検証、またはデータフィルタリングを構築しているかどうかにかかわらず、このアプローチは複雑さを軽減し、コードの読みやすさを向上させます。 Laravelの他の文字列ヘルパー関数と組み合わせて、効率的な文字列操作のための包括的なツールキットを形成します。 doesntContain
以上がLaravel&#039;の文字列の不在を確認しませんの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。