Morteza Sahebkar Morteza Sahebkar - 3 months ago 10
PHP Question

How can I have different rules on pregmatch?

Let's assume we have couple of inputs:
1-west:726435632:9236

2-west:8463758:873648

green:8234682:92347

red:98374:98374

H09

For output I want to have:

1. if it was anything other than H, just pass the first part like

1-west:283647873:86324873---->1-west


  1. if it was anything started with H, pass HOLDING as output:

    H06--->HOLDING



Can anybody help me how to implement this?

Answer

Something like this ?

function check($string) {
    $test = preg_match('/^H./', $text);
    if ($test) {
        return 'HOLDING';
    }
    $parts = explode(':', $string);
    return $parts[0];
}

Or in one line (assuming $input contains your input)

$output = preg_match('/^H./', $input) ? 'HOLDING' : substr($input, 0, strpos($input, ":"));
Comments