Search code examples
phpsubstring

Isolate leading string before one of two delimiting characters


How can I get the substring before the first space or dot?

Like

$string = "test.test"
//result = test

$string = "test doe"
//result = test

Sure I can use explode two times, but I am sure that's not the best solutions.


Solution

  • If you want to split on several different chars, take a look at preg_split

    //split string on space or period:
    $split=preg_split('/[ \.]/', $string);