Reputation: 173
I'm working with data migration using Drupal 7. I am migrating some taxonomy terms and I wanted to know how to remove spaces and commas from a sentence.
If this is the sentence:
' this, is my sentence'
The desired result that I'm looking for:
'thisismysentence'
So far I have managed to do this:
$terms = explode(",", $row->np_cancer_type);
foreach ($terms as $key => $value) {
$terms[$key] = trim($value);
}
var_dump($terms);
which only gives me the following result: 'this is my sentence' Anyone has a suggestion on how to achieve my desired result
Upvotes: 4
Views: 3144
Reputation: 76666
Just use str_replace()
:
$row->np_cancer_type = str_replace( array(' ',','), '', $row->np_cancer_type);
Example:
$str = ' this, is my sentence';
$str = str_replace( array(' ',','), '', $str);
echo $str; // thisismysentence
Upvotes: 4
Reputation: 786289
You can use one preg_replace
call to do this:
$str = ' this, is my sentence';
$str = preg_replace('/[ ,]+/', '', $str);
//=> thisismysentence
Upvotes: 8