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
You can use one preg_replace call to do this:
$str = ' this, is my sentence';
$str = preg_replace('/[ ,]+/', '', $str);
//=> thisismysentence
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With