php 将一个字符串分成两部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9891792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Split a string into two parts
提问by GSTAR
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";
$string = "one two three four five";
The first part needs to contain all of the words, except for the last one. The second part needs to contain just the last word.
第一部分需要包含所有单词,最后一个除外。第二部分只需要包含最后一个词。
Can anyone advise?
任何人都可以建议吗?
EDIT: The two parts need to be returned as strings, not arrays, e.g:
编辑:这两个部分需要作为字符串返回,而不是数组,例如:
$part1 = "one two three four";
$part1 = "one two three four";
$part2 = "five";
$part2 = "five";
回答by Marc B
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);
回答by codaddict
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);
回答by Bono
回答by Mark Baker
$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);
回答by Alex Turpin
Use strrpos
to get last space character's position, then substr
to divide the string with that position.
使用strrpos
获得最后一个空格字符的位置,然后substr
划分与该位置的字符串。
<?php
$string = 'one two three four five';
$pos = strrpos($string, ' ');
$first = substr($string, 0, $pos);
$second = substr($string, $pos + 1);
var_dump($first, $second);
?>
回答by user783322
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];
回答by nathanjosiah
$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 )
Array ( [0] => one two three four five [1] => one two three four [2] => five )
Then your parts would be $matches[1]
and $matches[2]
那么你的部分将是$matches[1]
和$matches[2]
回答by vangel paronov
my solution in Perl :)... PHP and Perl are similar :) $string="one five three four five";
我在 Perl 中的解决方案 :)... PHP 和 Perl 是相似的 :) $string="一五三四五";
@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";
回答by Rocket Hazmat
$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
回答by dan-lee
This should do it:
这应该这样做:
$arr = explode(' ', $string);
$second = array_pop($arr);
$result[] = implode(' ', $arr);
$result[] = $second;