12

I need to split a string into two parts. The string contains words separated by a space and can contain any number of words, e.g:

$string = "one two three four five";

The first part needs to contain all of the words except for the last word.

The second part needs to contain just the last word.

EDIT: The two parts need to be returned as strings, not arrays, e.g:

$part1 = "one two three four";

$part2 = "five";

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
MAX POWER
  • 5,213
  • 15
  • 89
  • 141
  • 1
    strrpos would be a good starting point. The manual has more. – GordonM Mar 27 '12 at 14:44
  • This is not the response for the exact question but related to the title because I was looking for that *almost* equal two parts and I achived that with using [wordwrap](http://php.net/manual/tr/function.wordwrap.php). – hayatbiralem Feb 27 '19 at 14:14

11 Answers11

27

Couple ways you can go about it.

Array operations:

$string ="one two three four five";
$words = explode(' ', $string);
$last_word = array_pop($words);
$first_chunk = implode(' ', $words);

String operations:

$string="one two three four five";
$last_space = strrpos($string, ' ');
$last_word = substr($string, $last_space);
$first_chunk = substr($string, 0, $last_space);
Farahmand
  • 2,731
  • 1
  • 24
  • 27
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Logically, as OP is using strings and not using arrays, I'd say use the "non" array option as they don't need an array (makes the code seem more logical as it's just working with a string) but is there any performance difference? – James Sep 12 '15 at 19:00
9

What you need is to split the input string on the last space. Now a last space is a space which is not followed by any more spaces. So you can use negative lookahead assertion to find the last space:

$string="one two three four five";
$pieces = preg_split('/ (?!.* )/',$string);
codaddict
  • 445,704
  • 82
  • 492
  • 529
5

Have a look at the explode function in PHP

Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter

Bono
  • 4,757
  • 6
  • 48
  • 77
3
$string="one two three four five";

list($second,$first) = explode(' ',strrev($string),2);
$first = strrev($first);
$second = strrev($second);

var_dump($first);
var_dump($second);
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
3

Use strrpos to get last space character's position, then substr to divide the string with that position.

<?php
    $string = 'one two three four five';
    $pos = strrpos($string, ' ');
    $first = substr($string, 0, $pos);
    $second = substr($string, $pos + 1);
    var_dump($first, $second);
?>

Live example

Alex Turpin
  • 46,743
  • 23
  • 113
  • 145
1
$string = "one two three four five";
$array = explode(" ", $string); // Split string into an array

$lastWord = array_pop($array); // Get the last word
// $array now contains the first four words
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
1

This should do it:

$arr = explode(' ', $string);
$second = array_pop($arr);
$result[] = implode(' ', $arr);
$result[] = $second;
dan-lee
  • 14,365
  • 5
  • 52
  • 77
1

Something like this would do it, although it's not particularly elegant.

$string=explode(" ", $string);
$new_string_1=$string[0]." ".$string[1]." ".$string[2]." ".$string[3];
$new_string_2=$string[4];
user783322
  • 479
  • 1
  • 8
  • 19
1
$string="one two three four five";
$matches = array();
preg_match('/(.*?)(\w+)$/', $string, $matches);
print_r($matches);

Output:

Array ( [0] => one two three four five [1] => one two three four [2] => five )

Then your parts would be $matches[1] and $matches[2]

nathanjosiah
  • 4,441
  • 4
  • 35
  • 47
1

my solution in Perl :)... PHP and Perl are similar :) $string="one five three four five";

@s = split(/\s+/, $string) ;

$s1 = $string ;
$s1 =~ s/$s[-1]$//e ;

$s2 = $s[-1] ;
print "The first part: $s1 \n";
print "The second part: $s2 \n";
0

Greedily traverse the characters in the input string then match the latest occurring space and split on that. Super easy, no lookaround or captures or any other reasons to slow down the regex engine.

\K means "forget all matched characters up to this point".

Code: (Demo)

$string = "one two three four five";
var_export(
    preg_split('/.*\K /', $string)
);

Output:

array (
  0 => 'one two three four',
  1 => 'five',
)

Or as individual variables: (Demo)

[$part1, $part2] = preg_split('/.*\K /', $string);
var_export($part1);
echo "\n";
var_export($part2);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136