Home >Backend Development >PHP Tutorial >php中替换字符串中的空格为逗号','的方法_PHP

php中替换字符串中的空格为逗号','的方法_PHP

WBOY
WBOYOriginal
2016-06-01 11:53:07963browse

今天在网查到一篇介绍php中替换字符串中的空格为逗号','的文章,作个日记保存下来。
复制代码 代码如下:

 php <br>/* <br>* 关键词中的空格替换为',' <br>*/ <br>public function emptyreplace($str) { <br>$str = str_replace(' ', ' ', $str); //替换全角空格为半角 <br>$str = str_replace(' ', ' ', $str); //替换连续的空格为一个 <br>$noe = false; //是否遇到不是空格的字符 <br>for ($i=0 ; $i<strlen>if($noe && $str[$i]==' ') $str[$i] = ','; //如果当前这个空格之前出现了不是空格的字符 <br>elseif($str[$i]!=' ') $noe=true; //当前这个字符不是空格,定义下 $noe 变量 <br>} <br>return $str; <br>} <br>?> <br>
    </strlen>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn