修剪似乎不适用于PHP

Dra*_*Vet 3 php


我是trimPHP 的功能的粉丝.但是,我想我遇到了一个奇怪的障碍.我有一个名为keys的字符串,其中包含:"mavrick,ball,bouncing,food,easy mac",并执行此功能

// note the double space before "bouncing"
$keys = "mavrick, ball,  bouncing, food,  easy mac, ";
$theKeywords = explode(", ", $keys);
foreach($theKeywords as $key){
  $key = trim($key);
}
echo $theKeywords[2];
Run Code Online (Sandbox Code Playgroud)

但是在这里,输出是"弹跳"而不是"弹跳".trim这里使用的功能不正确吗?

编辑:
我的原始字符串在"弹跳"之前有两个空格,由于某种原因它不想显示.我尝试用foreach引用它($ theKeywords as&$ key)但是它引发了一个错误.

rek*_*ire 5

问题是您使用副本而不是原始值.改为使用引用:

$theKeywords = explode(", ", $keys);
foreach($theKeywords as &$key){
  $key = trim($key);
}
echo $theKeywords[2];
Run Code Online (Sandbox Code Playgroud)