0

I have this string "test1.base-loc.com" which is the value inside the argument $baseURL. I'd like to get the "test1" using RegEx and compare the output to match to the given cases in my switch function. How can I do that properly?

    private function getAppID($baseURL){
    $re = '/(\w)*/m';
    $str = $baseURL;
    $to_return;

    preg_match($re, $str, $matches, PREG_OFFSET_CAPTURE);

    switch($matches[0]){
        case 'test1':
        $to_return = 2;
        break;

        case 'test2':
        $to_return = 3;
        break;

        case 'test3':
        $to_return = 4;
        break;

        case 'test4':
        $to_return = 5;
        break;

        default:
        $to_return = 1;
        break;
    }
    return $to_return;
}
4
  • Why regex? you can use explode() if all you want is the first part of the domain.
    – Spoody
    Commented Jul 20, 2018 at 17:48
  • thanks @Mehdi I will try that one out Commented Jul 20, 2018 at 17:50
  • or even preg_split() php.net/manual/en/function.preg-split.php
    – Edward
    Commented Jul 20, 2018 at 17:54
  • Note that with the option PREG_OFFSET_CAPTURE, $matches[0] is no more a string but an array with the whole match as first item and the offset as second item. Commented Jul 20, 2018 at 18:15

2 Answers 2

1

In my opinion an explode with an array is way shorter and cleaner

private function getAppId($url)
{        
    $subdomain = explode('.', $url);

    $ids = [
        'test1' => 2,
        'test2' => 3,
        'test3' => 4,
        'test4' => 5,
    ];

    // Return the ID if it exists or 1
    return $ids[$subdomain[0]] ?? 1;
}

Be aware that this will get sub in sub.test.domain.com, so this is not the best way to parse a URL, only use this if you are 100% sure that the input is going to be sub.domain.com or this won't work.

The ?? is the null coalescing operator it was introduced in PHP 7

4
  • wait... what if there is no subdomain. ie http://example.com, he will get (string) example....
    – Edward
    Commented Jul 20, 2018 at 17:57
  • @Edward he will actually get http://example, so this depends on the input. But this works with what OP gave in his question.
    – Spoody
    Commented Jul 20, 2018 at 18:00
  • 1
    yep, i deleted my answer, because stackoverflow.com/questions/5292937/…
    – Edward
    Commented Jul 20, 2018 at 18:05
  • @Edward the only way we can be sure is with OP's feedback.
    – Spoody
    Commented Jul 20, 2018 at 18:07
0

switch($matches[0][0]) does the trick. The extra index is necessary as a var_dump() of $matches reveals:

array(2) {
  [0]=>
  array(2) {
    [0]=>
    string(5) "test1"
    [1]=>
    int(0)
  }
  [1]=>
  array(2) {
    [0]=>
    string(1) "1"
    [1]=>
    int(4)
  }
}

As others point out, this is probably not the ideal way to solve the problem, though. There are many other ways, for example:

preg_match('/test([1-4])\./', $baseURL, $m);
return $m ? $m[1] + 1 : 1;

Not the answer you're looking for? Browse other questions tagged or ask your own question.