1

Can you help me? I'm trying to make a PHP Regex to accept only: A-Z, a-z, 0-9, (, ), ! and accented words(á,é,í,ó,ú and Á,É,Í,Ó,Ú) to filter a list.

I tried everyting and searched so much, and I'm not getting succcess... What should I do?

@edit:

if (!preg_match("/^[\p{L}-]*$/u", $line)){

I already tried using this from this thread but didn't worked.

What I'm trying to do? Accept only words that I want to filter this list: List

@edit2: Already tried convertind to UTF8, using iconv, mb_convert_encoding, etc...

ESP32
  • 8,089
  • 2
  • 40
  • 61
Amazing man
  • 35
  • 1
  • 7

3 Answers3

3

It think this is what you are looking for:

if (preg_match("/[a-zA-Z0-9áéíóúÁÉÍÓÚ\s]*/", $line)){
    // line is ok
}

You can test here: https://regex101.com/r/4Ozxw2/1

ESP32
  • 8,089
  • 2
  • 40
  • 61
  • I was kind of surprised how well this approach worked for a registered trademark symbol as well. – Mike_K Mar 24 '22 at 21:16
0

Something like

<?php

$strings = ['hash#', 'percent%', '!exc', 'ó','-',',','num1'];

foreach($strings as $v) {
    if (preg_match("/^[\p{L}A-Za-z0-9,!]*$/u", $v)) {
        print $v . '<br/>';
    }
}
0

Use this...

<?php

$strings = ['test(yes)h#', 'test2%', '!test3', 'ó','-',',...','test'];

foreach($strings as $v) {
    if (preg_match("/^[\p{L}A-Za-z0-9! | #\((.*?)\)#]*$/u", $v)) {
        print $v . '<br/>';
    }
}

?>

I think this will solve your issue.

Sarvan Kumar
  • 926
  • 1
  • 11
  • 27