Home > Article > Backend Development > How to replace spaces in strings with commas ',' in PHP_PHP Tutorial
Today I found an article on the Internet that introduces replacing spaces in strings with commas ',' in PHP. I made a diary and saved it.
php
/*
* Replace spaces in keywords with ','
*/
public function emptyreplace($str) {
$str = str_replace(' ', ' ', $str); //Replace full-width spaces with half-width
$str = str_replace(' ', ' ', $str); //Replace consecutive spaces with one
$noe = false; //Whether encountered Characters that are not spaces
for ($i=0; $iif($noe && $str[$i]==' ') $str[$i] = ','; //If a character that is not a space appears before the current space
elseif($str[$i]!=' ') $noe=true; //Current This character is not a space. Define the $noe variable
}
return $str;
}
?>