3

我正在使用 Drupal 7 进行数据迁移。我正在迁移一些分类术语,我想知道如何从句子中删除空格和逗号。

如果是这样的句子:

'这,是我的句子'

我正在寻找的预期结果:

'thisismysentence'

到目前为止,我已经设法做到这一点:

$terms = explode(",", $row->np_cancer_type);
    foreach ($terms as $key => $value) {
      $terms[$key] = trim($value);
    }
var_dump($terms);

这只会给我以下结果:“这是我的句子”任何人都对如何达到我想要的结果有建议

4

2 回答 2

8

You can use one preg_replace call to do this:

$str = ' this, is my sentence';
$str = preg_replace('/[ ,]+/', '', $str);
//=> thisismysentence
于 2013-10-29T15:10:18.060 回答
3

只需使用str_replace()

$row->np_cancer_type = str_replace( array(' ',','), '', $row->np_cancer_type);

例子:

$str = ' this, is my sentence';
$str = str_replace( array(' ',','), '', $str);
echo $str; // thisismysentence
于 2013-10-29T15:12:17.067 回答