PHP 在特定字符处剪切字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/14601364/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Cut String At specific character
提问by user1949029
$string = "aaa, bbb, ccc, ddd, eee, fff";
I would like to cut string after third , so i would like to get output from string:
我想在第三个之后切断字符串,所以我想从字符串中获取输出:
aaa, bbb, ccc
回答by Teun Lassche
You can use strpos()and substr()for this. See 
为此,您可以使用strpos()和substr()。看
- http://php.net/strpos
 $string = substr($string, 0, strpos($string, ', ddd'));
- http://php.net/strpos
 $string = substr($string, 0, strpos($string, ', ddd'));
Alternate approach using explode:
使用爆炸的替代方法:
$arr = explode(',', $string);
$string = implode(',',array_slice($arr, 0, 3);
回答by Laurence
$x = explode(',', $string);
$result = "$x[0], $x[1], $x[2]";
回答by Rikesh
回答by mamdouh alramadan
If you don't know exactly the number chars to count I would suggest an ImplodeExplodelike this:
如果您不确切知道要计算的字符数,我会建议ImplodeExplode这样的:
$string = "aaa, bbb, ccc, ddd, eee, fff";
$arr = explode(',' , $string);
$out = array();
for($i = 0; $i < 3; $i++)
{
  $out[] = $arr[$i];
}
$string2 = implode(',', $out);
echo $string2; // output is: aaa, bbb, ccc
Update
更新
here's a phpfiddle
这是一个phpfiddle
回答by Parimal Raj
$string = "aaa, bbb, ccc, ddd, eee, fff";
$arr = explode(", ", $string);
$arr = array_splice($arr, 0, 3);
$string = implode($arr, ", ");
echo $string; // = "aaa, bbb, ccc"
回答by Miky
You can use explode() and implode() PHP functions to get it.
您可以使用explode() 和implode() PHP 函数来获取它。

