2013-10-29 73 views
3

我正在使用Drupal 7進行數據遷移。我正在遷移一些分類術語,我想知道如何從句子中刪除空格和逗號。刪除句子中的所有空格和逗號

如果是這樣的句子:

'這是我的一句'

所需結果是我在尋找:

'thisismysentence'

到目前爲止我設法做到這一點:

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

只給了我以下結果: 「這是我的一句」 任何人有關於如何實現我的所需結果

+0

我不知道php,但是如果你可以分割'/ [\ s \ pP] + /'然後加入結果數組? – sln

回答

6

您可以使用一個preg_replace調用爲此建議:

$str = ' this, is my sentence'; 
$str = preg_replace('/[ ,]+/', '', $str); 
//=> thisismysentence 
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