Method of replacing spaces in strings with commas' 'In php

  • 2021-06-28 12:00:03
  • OfStack

Today I found an article in the web about comma','In the substitution string of php and saved it as a journal.
 
<pre name="code" class="php"><? php 
/* 
*  Replace spaces in keywords with ',' 
*/ 
public function emptyreplace($str) { 
$str = str_replace('   ', ' ', $str); // Replace full space with half space  
$str = str_replace(' ', ' ', $str); // Replace consecutive spaces with 1 individual  
$noe = false; // Whether to encounter a character that is not a space  
for ($i=0 ; $i<strlen($str); $i++) { // Traverse entire string  
if($noe && $str[$i]==' ') $str[$i] = ','; // If a character that is not a space appears before the current space  
elseif($str[$i]!=' ') $noe=true; // The current character is not a space, defined as  $noe  variable  
} 
return $str; 
} 
?> 

Related articles: