简体   繁体   English

PHP Regex仅接受特定字符

[英]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 , ( , ) , ! 我试图做一个PHP正则表达式只接受: 包括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... @ edit2:已经尝试使用iconv,mb_convert_encoding等将convertind转换为UTF8 ...

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 您可以在这里进行测试: 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. 我认为这可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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