dpr26232 2013-05-02 07:23
浏览 506
已采纳

只允许英文字符/字母/数字和一些特殊字符[重复]

My problem is that I am making a small search engine from scratch, but it gets messed up if I search in Russian/any other language besides English. I was hoping some one could give me a code with regex that could filter out (not just detect, automaticallt filter out) Russian letters, or any other letters except the English letters, and keyboard special characters (-/:;()$&@". - etc). Later on, I will implement different language support for my engine, but for now, I want to finish the base of the engine.

Thanks in advance.

</div>
  • 写回答

3条回答 默认 最新

  • drip5880 2013-05-02 07:49
    关注

    You may create an array of allowed characters and then filter those that are not allowed:

    $allowed = array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9), array(' ', '+', '/', '-', '*', '.')); // Create an array of allowed characters
    
    $string = 'This is allowed and this not é Ó ½ and nothing 123.'; // test string
    
    $array = str_split($string); // split the string (character length = 1)
    
    echo implode('', array_intersect($array, $allowed)); // Filter and implode !
    

    Online demo.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改