爆炸字符串,仅在爆炸匹配的最后一次出现时

时间:2016-03-18 20:57:16

标签: php explode

我试图爆炸一个字符串,但我只需要在最后'and'而不是每个'and'进行爆炸。有没有办法做到这一点?

<?php

$string = "one and two and three and four and five";
$string = explode(" and ", $string);

print_r($string);

?>

结果:

数组([0] =&gt;一[1] =&gt;二[2] =&gt;三[3] =&gt;四[4] =&gt;五)

需要结果:

数组([0] =&gt;一,二,三,四[1] =&gt;五)

6 个答案:

答案 0 :(得分:5)

这似乎很容易使用基本的字符串函数。

$x = strrpos($string, ' and ');
$s2 = array(substr($string, 0, $x), substr($string, $x + 5));

答案 1 :(得分:1)

我的方法更简单一点,使用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'

这里唯一的问题是第一场比赛还有一个尾随&#39;和&#39;如果需要,可以通过一些简单的编码来摆脱它。如果你想要更复杂的正则表达式,你可以使用积极的前瞻和消极的外观。

答案 2 :(得分:1)

您还可以使用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的字符。第一个?会阻止捕获其他组。

答案 3 :(得分:0)

我不知道是否有其他快速方法可以解决这个问题,但您可以尝试下面的代码;

$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

希望这有帮助。

答案 4 :(得分:0)

写了一个函数来做到这一点:

<?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)

?>

答案 5 :(得分:0)

这应该有效:

$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

相关问题