M
M
maksim-mshp2020-05-26 15:32:15
PHP
maksim-mshp, 2020-05-26 15:32:15

Translate regular expression in PHP?

Hello. There are functions for checking e-mail and phone number for validity.

function ValidMail(n) {
    var re = /^[\w-\.][email protected][\w-]+\.[a-z]{2,4}$/i;
    var valid = re.test(n);
    return valid;
}
 
function ValidPhone(n) {
    var re = /^((8|\+7)[\- ]?)?(\(?\d{3}\)?[\- ]?)?[\d\- ]{7,10}$/i;
    var valid = re.test(n);
    return valid;
}


How to work with regular expressions in PHP? Do they have the same syntax as in JS? Please help me rewrite these functions.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ivan Rodichev, 2020-05-26
@maksim-mshp

The syntax is the same

$foo = '[email protected]';
preg_match('/^[\w-\.][email protected][\w-]+\.[a-z]{2,4}$/i', $foo, $matches);
echo $matches[0];

A
Arseny, 2020-05-26
Matytsyn @ArsenyMatytsyn

Regular expressions themselves are universal for programming languages. But the syntax of their declaration and record is somewhat different. In JS, you can enter, for example, as a string with escaping, or a RegExp constructor . In PHP I don't remember there being any difference (in terms of escaping), but you should refer directly to the documentation for that.
At the same time, as SagePtr correctly noted in the comments, the service configurator allows you to check both the regular expression and get its form in the PL. Pay attention to the panel on the left.

A
Anatoly Tsybin, 2020-05-26
@Tsybinn

here is a good material on regular expressions php old.code.mu/books/php/regular/rabota-s-regularnym...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question