Home  >  Article  >  Backend Development  >  Solution to PHP Warning: Invalid argument supplied for preg_match()

Solution to PHP Warning: Invalid argument supplied for preg_match()

WBOY
WBOYOriginal
2023-06-23 11:17:01903browse

When you use PHP's preg_match() function, you may encounter the "Invalid argument supplied for preg_match()" error. This error means that you did not provide the parameters to the preg_match() function correctly. In this article, we will learn more about this error and how to fix it.

  1. Error background

In a PHP application, you may use the preg_match() function to search and match a regular expression. However, if you do not supply the arguments correctly as expected by the preg_match() function, you will encounter the following error message:

PHP Warning: Invalid argument supplied for preg_match()

This Meaning, you did not provide the parameters to the preg_match() function correctly. Specifically, you will encounter this error if you do not provide the correct regex parameters.

In addition, this error will also occur when the second parameter of the preg_match() function is not a string.

  1. Solution

Before solving this error, you need to understand the correct parameters required by the preg_match() function. The preg_match() function requires two parameters:

  • Parameter 1: a regular expression
  • Parameter 2: the string to search in

Here are some common problems and solutions that may cause "Invalid argument supplied for preg_match()" errors:

  1. Regular expression argument not supplied

If you did not Provide a regular expression argument to the preg_match() function and you will see an "Invalid argument supplied for preg_match()" error.

For example, if you use the following code:

$string = "Hello, world!";
preg_match($string);

Running the code will result in the following Error: PHP Warning: Invalid argument supplied for preg_match()

To solve this problem, you need to provide a regular expression to the preg_match() function as follows:

$string = "Hello , world!";
preg_match("/Hello/", $string);

The above code will search for the "Hello" string in the $string variable. Here, the regular expression is "/Hello/".

  1. The second parameter is not a string

When the second parameter of the preg_match() function is not a string, "Invalid argument supplied for preg_match()" will appear "mistake.

For example, if you use the following code:

$string = 123;
preg_match("/[0-9] /", $string);

Running the code results in the following error: PHP Warning: Invalid argument supplied for preg_match()

To resolve this issue, you need to ensure that the second argument to the preg_match() function is a string. If what you are searching for is a number, you should convert it to a string like this:

$string = "123";
preg_match("/[0-9] /", $ string);

The above code will search for numbers in the $string variable. Here, the regular expression is "/[0-9] /".

  1. Other Errors

If you have correctly provided the regular expression and the string to search for but still see the "Invalid argument supplied for preg_match()" error , please make sure:

  • You are not using invalid syntax in the regular expression.
  • You have completely removed unnecessary whitespace in the regular expression.
  • Your PHP version supports the preg_match() function.
  1. Summary

In a PHP application, you might use the preg_match() function to search and match a regular expression. You may encounter "Invalid argument supplied for preg_match()" error when you do not supply the arguments to the preg_match() function correctly. In order to solve this problem, you need to make sure that you provide the regular expression and the string to search within correctly, and that there are no syntax errors or invalid spaces.

The above is the detailed content of Solution to PHP Warning: Invalid argument supplied for preg_match(). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn