Oihana PHP

isRegexp.php

Table of Contents

Functions

isRegexp()  : bool
Determines whether a given string is a valid regular expression pattern.

Functions

isRegexp()

Determines whether a given string is a valid regular expression pattern.

isRegexp(string $pattern) : bool

This function checks if the string can be safely used in a preg_match() call. It does not validate semantic correctness (e.g., useless patterns), only syntax.

⚠️ Internally uses the @ operator to suppress warnings from preg_match().

Parameters
$pattern : string

The string to test as a potential regex pattern.

Tags
example
isRegexp('/^[a-z]+$/');       // true
isRegexp('/[0-9]{3,}/');      // true
isRegexp('not a regex');      // false (missing delimiters)
isRegexp('/unterminated');    // false
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
bool

true if the string is a valid PCRE regular expression, false otherwise.


        
On this page

Search results