cleaning variables with preg_match()

Asked

Viewed 53 times

-2

How to use the function preg_match() to remove special characters and allow ., @ and accents?

If it is not possible to do with this function, what other can I use to have the same result?

Example:

$exemplo1=preg_match("[expressão]" , '/*João Paulo/');  
$exemplo2 = preg_match("[expressão]" , '[email protected]+');

$echo = $exemplo1 // João Paulo
echo = $exemplo2 // [email protected]
  • Could add an example of expected input and output?

  • updated the question of a look pf

3 answers

0

You can use this way, validate email, then password using the function preg_match

$email = "[email protected]";
$nome = "João Silva";
$regex_nome = '/^[a-zA-ZãÃáÁàÀêÊéÉèÈíÍìÌôÔõÕóÓòÒúÚùÙûÛçǺª\' \']+$/';
$regex_email = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
$email = (preg_match($regex_email, $email))?$email:"email inválido";
$nome = (preg_match($regex_nome, $nome))?$nome:"nome inválido";

var_dump($email);
var_dump($nome);

Can also be used the filter_var for this purpose, give a read.

0

Use the function preg_replace:

$exemplo1 = preg_replace ('/[\/*\&%#\$+-]/', '', '/*João Paulo/');
echo $exemplo1;

OUTPUT:

John Paul

Example 2:

$exemplo2 = preg_replace ('/[\/*\&%#\$+-]/', '', '[email protected]+');
echo $exemplo2;

OUTPUT:

[email protected]

-1

$nome = preg_replace('/[\*\&%#\$+-]/', '', $nome);
$email = preg_replace(/[^A-Za-z0-9\-\_]/, '', iconv('UTF-8', 'ASCII//TRANSLIT', $email));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.