更新:$ string可以有一个或多个“;”。我必须照顾最后一个。
$string = "Hello world; This is a nice day; after";
$out[] = trim( substr( strrchr ($string, ";"), 1 ) );
$out[] = trim( substr( strrchr ($string, ";"), 0 ) );
var_dump($out);
结果: 数组(2){ [0] => string(3)“after” [1] => string(5)“; after” }
但我需要的是:
array(2){ [0] => string(3)“after” [1] => string(5)“Hello world;这是美好的一天” }
我该怎么办?
答案 0 :(得分:6)
$dlm = "; "; $string = "Hello world; This is a nice day; after"; $split = explode($dlm,$string); $last = array_pop($split); $out = array($last,implode($dlm,$split));
答案 1 :(得分:2)
尝试
string = "Hello world; This is a nice day; after";
$out[] = trim(substr(strrchr($string, ";"), 1));
$out[] = trim(substr($string, 0, strrpos($string, ";")+1));
请参阅演示here
答案 2 :(得分:0)
您可以尝试:
$string = "Hello world; This is a nice day; after";
$parts = explode(';', $string);
$output = array(trim(array_pop($parts)), implode(';', $parts));
var_dump($output);
输出:
array (size=2)
0 => string 'after' (length=5)
1 => string 'Hello world; This is a nice day' (length=31)
答案 3 :(得分:0)
$string = "Hello world; This is a nice day; after";
$offset = strrpos($string,';');
$out[] = substr($string, $offset+1);
$out[] = trim(substr($string, 0, $offset));
print_r($out);
答案 4 :(得分:0)
$string = "Hello world; This is a nice day; after";
/// Explode it
$exploded_string = explode(";", $string);
/// Get last element
$result['last_one'] = end($exploded_string);
/// Remove last element
array_pop($exploded_string);
/// Implode other ones
$result['previous_ones'] = implode(";", $exploded_string);
print_r($result);
结果将是:
Array ( [last_one] => after [previous_ones] => Hello world; This is a nice day )