删除句子中的所有空格和逗号

R2-*_*her 3 php regex drupal drupal-7

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

如果是这句话:

'这,是我的判决'

我正在寻找的理想结果:

'thisismysentence'

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

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

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

anu*_*ava 8

您可以使用一个preg_replace调用来执行此操作:

$str = ' this, is my sentence';
$str = preg_replace('/[ ,]+/', '', $str);
//=> thisismysentence
Run Code Online (Sandbox Code Playgroud)