dsgd4654674 2014-12-18 13:02 采纳率: 0%
浏览 14
已采纳

变形字符串到模式

I'm working on an issue where users (truck drivers in this case) use SMS to send in information about work status. I want to keep the keying simple as not all users have smart phones so I have adopted some simple short codes for their input. Here are some examples and their meanings:

  • P#123456-3 (This is for picking up load 123456-3)
  • D#456789-1 (For the dropping of load 456789-1)
  • L#345678-9 (Load 345678-9 is going to be late)

This is pretty simple but users (and truck drivers) being what they are will key the updates in somewhat deviant manners such as:

  • #D 456789-1
  • D# 456789 - 1
  • D#.456789-1 This load looks wet to me do weneed to cancelthis order

You can pretty much come up with a dozen other permutations and it's not hard for me to catch and fix those that I can imagine.

I mostly use regular expressions to test the input against all my imagined "bad" patterns and then extract what I assume are the good parts, reassembling them into the correct order.

It's the new errors that cause me problems so I got to wondering if there was a more generic method where I can pass a "pattern" and a "message" to a function that would do it's best to turn the "message" into something matching the "pattern".

My searches have not found anything that really fits what I'm trying to do and I'm not even sure if there is a good general way to do this. I happen to be using PHP for this implementation but any type of example should help. Do any of you have a method?

  • 写回答

4条回答 默认 最新

  • dppxp79175 2014-12-18 14:04
    关注

    Try something like this:

    function parse($input) {
        // Clean up your input: 'D#.456789 - 1 foo bar' to 'D 456789 1 foo far'
        $clean = trim(preg_replace('/\W+/', ' ', $input));
        // Take first 3 words.
        list($status, $loadId1, $loadId2) = explode(' ', $clean);
        // Glue back your load ID to '456789-1'
        $loadId = $loadId1 . '-' . $loadId2;
        return compact('status', 'loadId');
    }
    

    Example:

    $inputs = array(
        'P#123456-3',
        '#D 456789-1',
        'D# 456789 - 1',
        'D#.456789-1 This load looks wet to me do weneed to cancelthis order',
    );
    echo '<pre>';
    foreach ($inputs as $s) {
        print_r(parse($s));
    }
    

    Output:

    Array
    (
        [status] => P
        [loadId] => 123456-3
    )
    Array
    (
        [status] => D
        [loadId] => 456789-1
    )
    Array
    (
        [status] => D
        [loadId] => 456789-1
    )
    Array
    (
        [status] => D
        [loadId] => 456789-1
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号