简体   繁体   中英

Storing REGEX in array PHP

I'm using an array to store a regex expression like this:

private $rules = ['usuario' => ['regexp' => "^(?![_.])(?!.*[_.]{2})[a-zA-Z0-9._]+(?<![_.])$", 'length' => ['max'=>'30','min'=>'4']],
    'email' => ['length' => ['max'=>'70','min'=>'5']],
    'contrasena' => ['length' => ['max'=>'70','min'=>'4']]
];

When I try:

echo '/' . $this->rules['usuario']['regexp'] . '/';

I got:

/^(?![_.])(?!.*[_.]{2})[a-zA-Z0-9._]+(?

Can anybody explain this behavior?

The reason is that there are < symbols in the regexp, and in the browser they are interpreted as tags. To avoid such behavior, you can escape such characters via htmlspecialchars :

echo htmlspecialchars('/' . $this->rules['usuario']['regexp'] . '/');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM