separate string in two by given position

前端 未结 8 1093
萌比男神i
萌比男神i 2020-12-03 17:14
$string = \'Some string\';
$pos = 5;

...??...

$begging // == \'Some s\';
$end // == \'tring\';

What is the best way to separate string in two by

相关标签:
8条回答
  • 2020-12-03 18:04

    Regex solution (if you are into it):

    ...
    $string = 'Some string xxx xxx';
    $pos = 5;
    
    list($beg, $end) = preg_split('/(?<=.{'.$pos.'})/', $string, 2);
    
    echo "$beg - $end";
    

    Regards

    rbo

    0 讨论(0)
  • 2020-12-03 18:08

    Wordwrap works better in my opinion.

    $text = "The quick brown fox jumped over the lazy dog.";
    $newtext = wordwrap($text, 20, "<br />\n");
    echo $newtext;
    

    The above example will output:

    The quick brown fox<br />
    jumped over the lazy<br />
    dog.
    

    Another example:

    $text = "A very long woooooooooooord.";
    $newtext = wordwrap($text, 8, "\n", true);
    
    echo "$newtext\n";
    

    The above example will output:

    A very
    long
    wooooooo
    ooooord.
    
    0 讨论(0)
提交回复
热议问题