Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在第二个“后,剥去字符串中的所有内容”-&引用;发生了什么?_Php - Fatal编程技术网

Php 在第二个“后,剥去字符串中的所有内容”-&引用;发生了什么?

Php 在第二个“后,剥去字符串中的所有内容”-&引用;发生了什么?,php,Php,在字符“-”第二次出现后,如何剥离字符串中的所有内容 今天是星期五,明天是星期六 在这种情况下,我希望周六和最后一条带“周六”一起被删除 非常感谢您提供的任何帮助:)我似乎只能在第一个“-”之后删除所有内容。使用以查找第一个匹配项,然后再次使用以上一个值为单位的偏移选项来查找结束点。然后使用 爆炸怎么样 $parts = explode( '-', "Today is - Friday and tomorrow is - Saturday" ); echo $parts[0].'-'.$part

在字符“-”第二次出现后,如何剥离字符串中的所有内容

今天是星期五,明天是星期六

在这种情况下,我希望周六和最后一条带“周六”一起被删除

非常感谢您提供的任何帮助:)我似乎只能在第一个“-”之后删除所有内容。

使用以查找第一个匹配项,然后再次使用以上一个值为单位的偏移选项来查找结束点。然后使用


爆炸怎么样

$parts = explode( '-', "Today is - Friday and tomorrow is - Saturday" );
echo $parts[0].'-'.$parts[1];

另一种方法是:

可以使用explode()在每次出现“-”时分割字符串。例如:

将给您留下:

$parts = ["Today is", "Friday and tomorrow is", "Saturday"]

这样,您想要的位将是中间的一个“--”的前两个项目,因此我们可以从数组中弹出最后一个元素,并加入其余部分:

array_pop($parts);
$result = implode(" - ", $parts);
其中:

$result == "Today is - Friday and tomorrow is";

我得到了同样的问题,但是我需要从第三次出现的字符串中取回字符串,所以我为它创建了一个函数,在这个函数中可以给出一个唯一的分隔位置。可能其他人喜欢使用:

function strcut( $str, $char, $pos ) {
    $i = 1;
    $a = explode( $char, $str );
    $r = array();
    foreach ( $a as $b ) {
        if( $pos < $i  ) {
            $r[] = $b;
        }
        $i++;
    }

    return implode( $char, $r );
}
如果在结果之前需要分隔符,只需修改返回:

return $char . implode( $char, $r );

对于其他有同样问题的人;我使用了这个紧凑的解决方案,很容易调整

$str = 'Today is - Friday and tomorrow is - Saturday';

$sliceWith = "-"; // character to split by
$beginWith = 0; // 1 removes before first match, 0 will not
$splitAfter = 2; // number of matches to keep

$result = implode($sliceWith, array_slice(explode($sliceWith, $str), $beginWith, $splitAfter));

echo $result; // You might want to use trim($result)

不知道这个函数存在,@Felix!就像一个魅力和所有在一行。这一个是伟大的!需要一个小的解释,“strtok()”是如何工作的,因为这对我来说也是一个发现。但结果是好的和闪亮的!从php.net:
注意,只有对strtok的第一次调用使用字符串参数。每次对strtok的后续调用都只需要使用令牌,因为它跟踪它在当前字符串中的位置。
这就是我所说的解决方案。棒极了,伙计,谢谢这是一种高压手段。我建议您使用
explode
的第三个参数(限制)。将限制设置为超出需要的一个
pop
数组中的最后一个元素,然后
内爆
。没有循环,没有计数器。完成。
$result == "Today is - Friday and tomorrow is";
function strcut( $str, $char, $pos ) {
    $i = 1;
    $a = explode( $char, $str );
    $r = array();
    foreach ( $a as $b ) {
        if( $pos < $i  ) {
            $r[] = $b;
        }
        $i++;
    }

    return implode( $char, $r );
}
$str = "root/main/subfolder/photos/123.jpg";
$char = "/";
$pos = 3;
echo strcut($str, $char, $pos);
return $char . implode( $char, $r );
$str = 'Today is - Friday and tomorrow is - Saturday';

$sliceWith = "-"; // character to split by
$beginWith = 0; // 1 removes before first match, 0 will not
$splitAfter = 2; // number of matches to keep

$result = implode($sliceWith, array_slice(explode($sliceWith, $str), $beginWith, $splitAfter));

echo $result; // You might want to use trim($result)