我有一个这样的字符串:
S="str1|str2|str3"我想从S中提取另一个字符串,它将只包含
t="str1|str2"其中|是分隔符
谢谢
发布于 2011-11-23 12:17:09
$string = "str1|str2|str3";
$pieces = explode( '|', $string); // Explode on '|'
array_pop( $pieces); // Pop off the last element
$t = implode( '|', $pieces); // Join the string back together with '|'或者,使用字符串操作:
$string = "str1|str2|str3";
echo substr( $string, 0, strrpos( $string, '|'));Demo
发布于 2011-11-23 12:17:19
implode("|", array_slice(explode("|", $s), 0, 2));这不是一个非常灵活的解决方案,但适用于您的测试用例。
或者,您可以使用explode()的第三个参数limit,如下所示:
implode("|", explode("|", $s, -1));发布于 2011-11-23 12:17:37
$s = 'str1|str2|str3';
$t = implode('|', explode('|', $s, -1));
echo $t; // outputs 'str1|str2'https://stackoverflow.com/questions/8237238
复制相似问题