简体   繁体   中英

PHP Regex to accept only specific characters

Can you help me? I'm trying to make a PHP Regex to accept only: AZ , az , 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...

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

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/>';
    }
}

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.

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