Home >Backend Development >PHP Tutorial >Why Does My PHP `preg_match()` Function Throw a 'No Ending Delimiter' Error?

Why Does My PHP `preg_match()` Function Throw a 'No Ending Delimiter' Error?

Susan Sarandon
Susan SarandonOriginal
2024-12-17 22:34:18683browse

Why Does My PHP `preg_match()` Function Throw a

PHP Regular Expressions: Delimiter Error

Issue:

When executing the following PHP code, an error is encountered:

$pattern = "^([0-9]+)$";

if (preg_match($pattern, $input))
   echo "yes";
else
   echo "nope";

The error message reads: "Warning: preg_match() [function.preg-match]: No ending delimiter '^' found in."

Investigation:

The error suggests that the regular expression pattern lacks an ending delimiter, denoted by the caret symbol "^".

Solution:

In PHP, regular expression strings require delimiters. To resolve the issue, enclose the pattern in delimiters:

$numPattern = "/^([0-9]+)$/";

Additional Considerations:

  1. Verify that the letter "o" in the pattern is lowercase, as opposed to the number zero (0).
  2. If the goal is solely to validate the input, the capturing group can be removed and the regex simplified to:
/^\d+$/

For further information, refer to the documentation on PHP Delimiters.

The above is the detailed content of Why Does My PHP `preg_match()` Function Throw a 'No Ending Delimiter' Error?. 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