douyanjing8287 2012-01-06 23:15
浏览 22
已采纳

正则表达式在PHP中拆分字母数字,货币和数字术语

I am trying to split a string into terms in PHP using preg_split. I need to extract normal words ( \w ) but also currency ( even currency symbol ) and numeric terms ( including commas and decimal points ). Can anyone help me out, as I cannot seem to create a valid regex to use for preg_split to achieve this. Thanks

  • 写回答

3条回答 默认 最新

  • dqz30992 2012-01-06 23:41
    关注

    Why not use preg_match_all() instead of preg_split() ?

    $str = '"1.545" "$143" "$13.43" "1.5b" "hello" "G9"'
      . ' This is a test sentence, with some. 123. numbers'
      . ' 456.78 and punctuation! signs.';
    
    $digitsPattern = '\$?\d+(\.\d+)?';
    $wordsPattern = '[[:alnum:]]+';
    
    preg_match_all('/('.$digitsPattern.'|'.$wordsPattern.')/i', $str, $matches);
    
    print_r($matches[0]); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 QTableWidget重绘程序崩溃
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含