Divide the line into two parts

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

$string = "one two three four five";

The first part should contain all the words except the last. The second part should contain only the last word.

Can anyone advise?

EDIT: two parts should be returned as strings, not arrays, for example:

$part1 = "one two three four";

$part2 = "five";

+8
php
source share
10 answers

A couple of ways you can do this.

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); 
+21
source share

You need to split the input line at the last space. Now the last space is a space followed by no spaces. So you can use a negative lookahead statement to find the last space:

 $string="one two three four five"; $pieces = preg_split('/ (?!.* )/',$string); 
+7
source share

Take a look at explode in PHP

Returns an array of strings, each of which is a substring of a string formed by splitting it into borders formed by a line separator

+5
source share
 $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); 
+2
source share
 $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 
+1
source share

This should do it:

 $arr = explode(' ', $string); $second = array_pop($arr); $result[] = implode(' ', $arr); $result[] = $second; 
+1
source share

Use strrpos to get the last position of the space character, then substr to split the string by 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

+1
source share

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

 $string=explode(" ", $string); $new_string_1=$string[0]." ".$string[1]." ".$string[2]." ".$string[3]; $new_string_2=$string[4]; 
+1
source share
 $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 will be $matches[1] and $matches[2]

+1
source share

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"; 
+1
source share

All Articles