preg_match() expects parameter 2 to be string, array given Error

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# Solving `preg_match()` Expects String: Debugging Array Handling in Laravel As a senior developer, I frequently encounter issues where the expectation of data types clashes with the actual data received from user input or framework requests. The error message `preg_match() expects parameter 2 to be string, array given` is a classic symptom of this mismatch. It tells us exactly what went wrong: a function designed to work on single strings (like regular expression matching) was fed an entire collection (an array) instead. This post will dissect the scenario you presented—handling multiple form inputs in a Laravel context—explain the root cause of this error, and demonstrate the correct way to structure your data processing to avoid such type errors. We will ensure your data flows smoothly, adhering to best practices for handling requests in Laravel. --- ## The Root Cause: String vs. Array Mismatch The core problem lies in the fundamental difference between a single string and an array in PHP (and consequently, in functions like `preg_match`). 1. **String:** A string is a sequence of characters (e.g., `"hello"`). Functions expecting a string, such as `preg_match()`, require this linear sequence to perform pattern matching against it. 2. **Array:** An array is a collection of values indexed by integers (e.g., `['item1', 'item2']`). When you pass an array where a single string is expected, the function throws an error because it doesn't know how to interpret multiple data points simultaneously in that context. In your specific case, although the provided code snippet shows you successfully iterating over arrays derived from form inputs (`$request->description`), the error indicates that at some point *during* the execution—likely within a validation rule or a custom processing function that uses `preg_match` on the request data—you inadvertently passed an entire array instead of the specific string value you needed to match. ## Analyzing Your Data Flow and Controller Logic Let's look closely at how you are handling the incoming data based on your example: **Blade Input Setup:** ```html {!! Form::text('description[]',null,['class' => 'input-field input-sm','v-model' => 'row.description']) !!} {!! Form::text('log_time[]',null,['class' => 'input-field input-sm','v-model' => 'row.log_time']) !!} ``` This setup correctly generates multiple inputs for each row, resulting in arrays when submitted. **Controller Processing:** ```php $this->validate($request, $this->rules); $data = array(); foreach($request->description as $key => $value){ $data[]=[ 'description' => $value, 'log_time' => $request->log_time[$key], 'call_id' => $call->id, ]; } PortLog::create($data); ``` Your loop structure is fundamentally correct for iterating over multiple related inputs. When you execute `foreach($request->description as $key => $value)`, the `$value` variable holds a single string (e.g., `"des"` or `"hi"`) for each iteration, which is exactly what you need to insert into your new record. The error must be occurring in a different part of your code where you attempt to apply a pattern match to the *entire* `$request->description` array instead of iterating through it first. ## The Solution: Iteration is Key To resolve the `preg_match()` error, you must ensure that any function requiring a string input receives only a string. If you need to perform regex matching across multiple values, you must iterate over the array and apply the matching logic to each individual string. Here is the corrected conceptual approach for processing your data: ```php // Assume $request contains the submitted form data $dataToInsert = []; // Iterate through the description array foreach ($request->description as $index => $descriptionValue) { // Check if a specific regex operation needs to be performed on this single string if (preg_match('/^des.*$/', $descriptionValue)) { // If you were trying to validate or process the description itself: $isValid = true; // Success state } else { $isValid = false; // Failure state } // Now, construct the record using the individual string value $dataToInsert[] = [ 'description' => $descriptionValue, 'log_time' => $request->log_time[$index], // Ensure you use the correct index for log_time 'call_id' => $call->id, // Add validation results if necessary 'is_valid' => $isValid, ]; } PortLog::create($dataToInsert); ``` ### Best Practice: Leveraging Laravel Eloquent When dealing with complex data structures like this, remember that Laravel excels at abstracting away repetitive loop logic. Instead of manually building a flat array using `foreach`, consider mapping your request data directly into Eloquent models or collections. This keeps your controller cleaner and more resilient, which is a core principle when developing robust applications on the **Laravel** framework. ## Conclusion The error `preg_match() expects parameter 2 to be string, array given` is a clear signal that you are feeding an array where a single string is mandated. While your initial data collection loop was functional for creating the final database records, the error points to an operation happening *before* or *during* the processing of those values where a function expecting a string (like `preg_match`) encountered an entire array. The solution is always to isolate the data. Always iterate over arrays and operate on each element individually to ensure type safety and prevent these frustrating runtime errors. By focusing on iterating through `$request->description` one string at a time, you guarantee that every function receives exactly what it expects.