php explode function for only first white space

前端 未结 5 2066
执笔经年
执笔经年 2021-02-18 19:25

I have a String Hello This is a String. I need to explode it in PHP only for the First White Space. How is that possible?

相关标签:
5条回答
  • 2021-02-18 19:46

    An alternative to explode

    $str   = 'Hello This is a String';
    $parts = preg_split('/(\s)/', $str, PREG_SPLIT_DELIM_CAPTURE);
    
    0 讨论(0)
  • 2021-02-18 19:48

    yes

    just call

    explode(' ',$s, 2)
    

    this will create an array with at most 2 elements

    0 讨论(0)
  • 2021-02-18 19:55

    Try

    explode(' ', $your_string, 2)

    See more: explode()

    0 讨论(0)
  • 2021-02-18 19:58

    I'm assuming you mean a space character for this answer. instead of thinking in terms of explode() you should think in terms of "find the first character and split the string there."

    $pos = strpos($inputString, ' ');
    $part1 = substr($inputString, 0, $pos);
    $part2 = substr($inputString, $pos+1);
    
    0 讨论(0)
  • 2021-02-18 20:10

    set limit parameter

    print_r(explode(' ', $str, 2));
    

    Reference

    0 讨论(0)
提交回复
热议问题