dpf5207 2011-05-26 08:53
浏览 213
已采纳

使用PHP获取字符串中的分隔标点符号

I have a product that is sold to multiple customers, each customer has its own unique product code derived from the my original product code e.g

My code: 1245-65

Customer 1: 1245/65

Customer 2: 1245.65

My question: Is there any way to analyse such a string and find what is separating its integers? My goal is to have a settings page where a demo customer code would be entered then all product codes would be derived from that example code. I'm sure PHP can handle this!

EXTRA INFO:

Sorry, I haven't given enough information. There might be a situation where the separator is an alphabetical quantity e.g 1245ABC65. I hate updating a question like this when so many people have given valid answers :( my fault.

  • 写回答

3条回答 默认 最新

  • douzhangshao6369 2011-05-26 09:04
    关注

    You may want to look for non numeric characters using preg_match_all

    preg_match_all('/[^0-9]/', '1245-95', $matches);
    print_r($matches);
    
    //Array ( [0] => Array ( [0] => - ) ) in the example
    

    With the updated question, you have to write :

    $str = '1245ABC65';
    preg_match("/\d+([^0-9]+)\d+/", $str, $separator);
    echo $separator = $separator[1];
    

    or

    preg_match_all('/[^0-9]+/', '1245ABC95', $matches);
    print_r($matches);
    
    //Array ( [0] => Array ( [0] => 'ABC' ) ) in the example
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)