Search code examples
phpstringsplitdelimited

Split a string in half by a delimiting character


I am trying to split a string in PHP however it's not as easy as it is in C#, it seems to be a tad bit messy...

I have a string, which looks something like this:

blahblah1323r8b|7.45

and I would like to be able to access the results of the split like this:

$var = split_result['leftside'];
$var = split_result['rightside'];

Is this easy to do in PHP? I'm trying to find some good examples, but the ones I've seen seem to be not what I'm trying to do, and are a little over complicated.


Solution

  • To get an array:

    $array = explode('|', $str);
    

    Or to directly get two parts:

    list($left, $right) = explode('|', $str);
    

    See explode in the PHP manual.