duandun3178 2012-09-04 14:05
浏览 35
已采纳

Preg_replace和iso-8859-1字符匹配[关闭]

I have a problem with the preg_replace function.

In my code i will use it to delete all chars that are not letters or numbers at the beginning of a string.

This works well, but when the $string contains iso-8859-1 character like " è, ò, à, ù, é, ì " there are considered not words, and all were removed.

preg_replace('/^[^a-zA-Z0-9]+/', '', $string);

ie. If $string contains èxample the output will be xample.

I need that these iso-8859-1 vowels are included.

Anyone have a solution to this?

  • 写回答

2条回答 默认 最新

  • douludi8413 2012-09-04 14:13
    关注

    PCRE does not support Unicode blocks (in which case things would be much easier), so you really have no option other than to specify the set of allowed characters (or its negation) manually. The regex would look like

    [^a-zA-Z0-9\xC0-\xFF]+
    

    The problem is that the range \xC0-\xFF also includes undesirable characters (e.g. the division sign, \xF7), so you have to break it down into acceptable subranges depending on your requirements. Look at the codepage layout to help decide which characters are OK and which are not.

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

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真