drb88830 2010-12-17 03:10
浏览 37
已采纳

如何确保字符串仅包含预定义字符的预定义列表中的字符?

Let's say I have an HTML page with a text field that ask the user to enter a word. Then, I use the $_POST[] variable in my PHP script to play with the variable and do some stuff. My page and my PHP scripts would work using the UTF-8 encoding so anything can be entered by the user.

What I would like to know, it's how can I make sure that all characters contained in the word entered by the user are part of a predefined list of characters?

For example, the list would contain the characters 0 to 9, a to z, A to Z, but also some other characters like ü, ù, ô, é, à, ẑ and so on. What I don't want, it's characters like from the Russian alphabet like а, з, б, у, etc and other kinds of exotic characters.

I could use regular expression, but is it the most suitable solution? Isn't too slow? What about an array with all the characters? Is it me or it sounds very slow to loop through each character of a word and also loop through the array of accepted characters and test each combination.

Do you know a good solution?

Thanks.

  • 写回答

1条回答 默认 最新

  • dream_high1026 2010-12-17 03:27
    关注

    RE's are fast, and they are the way to go. Create a list of the characters you want, and then invert the sense of match, as shown below.

    if re.match(".*[^abcA-Z]", "d"):
      print "Your string has illegal characters"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测