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
Release: 2024-12-17 22:34:18
Original
616 people have browsed it

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";
Copy after login

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]+)$/";
Copy after login

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+$/
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template