分解字符串,仅在分解匹配的最后一次出现时
Exploding a string, only at the last occurrence of the explode match
我正在尝试分解一个字符串,但我需要它只在最后一个 'and'
而不是每个 'and'
分解。有没有办法做到这一点?
<?php
$string = "one and two and three and four and five";
$string = explode(" and ", $string);
print_r($string);
?>
结果:
数组([0]=>一[1]=>二[2]=>三[3]=>四[4]=>五)
需要结果:
数组([0] => 一二三四 [1] => 五)
我不知道是否有任何其他快速解决该需求的方法,但您可以尝试下面的代码;
$string = "one and two and three and four and five";
$stringArray = explode(" and ", $string);
$stringArrayItemCount = count($stringArray);
//Keep your last item
$stringArrayLastItem = $stringArray[$stringArrayItemCount-1];
//Remove last item from your array
unset($stringArray[$stringArrayItemCount-1]);
//Create new array imploding existing one + last item of your old array
$stringArray = array(implode(" and ",$stringArray),$stringArrayLastItem);
print_r($stringArray);
此示例的工作版本:http://ideone.com/qdZFtk
希望对您有所帮助。
写了一个函数来做到这一点:
<?php
$string = "one and two and three and four and five";
$string = explodeLast(" and ", $string);
echo $string;
function explodeLast($explodeAt, $string) {
$explode = explode($explodeAt, $string);
$count = count($explode);
$counter = 0;
$string = null;
while ($counter < $count-1) {
if ($counter < $count-2) {
$string .= $explode[$counter].$explodeAt;
} //end of if ($counter < $count-2)
else {
$string .= $explode[$counter];
} //end of else not ($counter < $count-2)
$counter++;
} //end of while ($counter < $count)
return $string;
} //end of function explodeLast($explode, $explodeAt)
?>
这应该有效:
$str = 'one and two and three and four and five';
$breakWord = ' and ';
$output = str_split($str, strrpos($str, $breakWord) + strlen($breakWord));
var_dump($output);
http://sandbox.onlinephpfunctions.com/code/0149b3ff973485befe97a1c6b241a6764bd2f289
编辑 - 正则表达式使用:
<?php
$str = 'one and two and three and four and the last part is actually longer than the first part';
$pattern = "/(.+) and (?!.* and )(.+)/";
preg_match($pattern, $str, $matches);
var_dump($matches);
http://sandbox.onlinephpfunctions.com/code/0bc68f46fe594e360134bdca256e5916a2f42f74
我的方法更加简单,使用 preg_match_all()
和一个简单的正则表达式代替:
$string = "one and two and three and four and five";
$pattern = '/(\w+\s)+/';
preg_match_all($pattern, $string, $matches);
$length = strlen($matches[0][0]);
echo $matches[0][0]; // 'one and two and three and four and'
echo substr($string, $length); // 'five'
这里唯一的问题是第一个匹配项仍然有尾随 'and',如果需要,可以通过一些简单的编码将其删除。如果您想要更复杂的正则表达式,您可以使用正向前瞻和负向后视。
这似乎很容易,只使用基本的字符串函数就可以做到。
$x = strrpos($string, ' and ');
$s2 = array(substr($string, 0, $x), substr($string, $x + 5));
你也可以使用preg_split
:
单字符定界符
$string = "one,two,three,four,five";
$delimiter = ",";
// The regexp will look like this: /,([^,]+)$/
$array = preg_split("/".$delimiter."([^".$delimiter."]+)$/", $string,
-1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
print_r($array);
正则表达式将最后一个定界符与最后一个项目匹配,但仅捕获该项目以便它保留在结果中,这要归功于 PREG_SPLIT_DELIM_CAPTURE。
PREG_SPLIT_NO_EMPTY 在那里是因为,我不确定为什么,拆分最后给出了一个空字符串。
多字符分隔符
此处必须调整正则表达式,使用负环视(如 this answer 中所述):
$string = "one and two and three and four and five";
$delimiter = " and ";
$array = preg_split("/$delimiter((?(?!$delimiter).)*$)/", $string,
-1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
print_r($array);
(?(?!$delimiter).)*
表示:只匹配不以$delimiter
开头的字符。第一个 ?
阻止捕获额外的组。
我正在尝试分解一个字符串,但我需要它只在最后一个 'and'
而不是每个 'and'
分解。有没有办法做到这一点?
<?php
$string = "one and two and three and four and five";
$string = explode(" and ", $string);
print_r($string);
?>
结果:
数组([0]=>一[1]=>二[2]=>三[3]=>四[4]=>五)
需要结果:
数组([0] => 一二三四 [1] => 五)
我不知道是否有任何其他快速解决该需求的方法,但您可以尝试下面的代码;
$string = "one and two and three and four and five";
$stringArray = explode(" and ", $string);
$stringArrayItemCount = count($stringArray);
//Keep your last item
$stringArrayLastItem = $stringArray[$stringArrayItemCount-1];
//Remove last item from your array
unset($stringArray[$stringArrayItemCount-1]);
//Create new array imploding existing one + last item of your old array
$stringArray = array(implode(" and ",$stringArray),$stringArrayLastItem);
print_r($stringArray);
此示例的工作版本:http://ideone.com/qdZFtk
希望对您有所帮助。
写了一个函数来做到这一点:
<?php
$string = "one and two and three and four and five";
$string = explodeLast(" and ", $string);
echo $string;
function explodeLast($explodeAt, $string) {
$explode = explode($explodeAt, $string);
$count = count($explode);
$counter = 0;
$string = null;
while ($counter < $count-1) {
if ($counter < $count-2) {
$string .= $explode[$counter].$explodeAt;
} //end of if ($counter < $count-2)
else {
$string .= $explode[$counter];
} //end of else not ($counter < $count-2)
$counter++;
} //end of while ($counter < $count)
return $string;
} //end of function explodeLast($explode, $explodeAt)
?>
这应该有效:
$str = 'one and two and three and four and five';
$breakWord = ' and ';
$output = str_split($str, strrpos($str, $breakWord) + strlen($breakWord));
var_dump($output);
http://sandbox.onlinephpfunctions.com/code/0149b3ff973485befe97a1c6b241a6764bd2f289
编辑 - 正则表达式使用:
<?php
$str = 'one and two and three and four and the last part is actually longer than the first part';
$pattern = "/(.+) and (?!.* and )(.+)/";
preg_match($pattern, $str, $matches);
var_dump($matches);
http://sandbox.onlinephpfunctions.com/code/0bc68f46fe594e360134bdca256e5916a2f42f74
我的方法更加简单,使用 preg_match_all()
和一个简单的正则表达式代替:
$string = "one and two and three and four and five";
$pattern = '/(\w+\s)+/';
preg_match_all($pattern, $string, $matches);
$length = strlen($matches[0][0]);
echo $matches[0][0]; // 'one and two and three and four and'
echo substr($string, $length); // 'five'
这里唯一的问题是第一个匹配项仍然有尾随 'and',如果需要,可以通过一些简单的编码将其删除。如果您想要更复杂的正则表达式,您可以使用正向前瞻和负向后视。
这似乎很容易,只使用基本的字符串函数就可以做到。
$x = strrpos($string, ' and ');
$s2 = array(substr($string, 0, $x), substr($string, $x + 5));
你也可以使用preg_split
:
单字符定界符
$string = "one,two,three,four,five";
$delimiter = ",";
// The regexp will look like this: /,([^,]+)$/
$array = preg_split("/".$delimiter."([^".$delimiter."]+)$/", $string,
-1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
print_r($array);
正则表达式将最后一个定界符与最后一个项目匹配,但仅捕获该项目以便它保留在结果中,这要归功于 PREG_SPLIT_DELIM_CAPTURE。
PREG_SPLIT_NO_EMPTY 在那里是因为,我不确定为什么,拆分最后给出了一个空字符串。
多字符分隔符
此处必须调整正则表达式,使用负环视(如 this answer 中所述):
$string = "one and two and three and four and five";
$delimiter = " and ";
$array = preg_split("/$delimiter((?(?!$delimiter).)*$)/", $string,
-1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
print_r($array);
(?(?!$delimiter).)*
表示:只匹配不以$delimiter
开头的字符。第一个 ?
阻止捕获额外的组。