Wins the offer (PHP version rewritten) ---- string

/ ** 
* replace spaces with each string "% 20 is"
*
* @param $ String String
* (must be greater than the string length after replacement) @param $ length character array length
* /
function ReplaceBlank ($ String, $ length) {

IF ($ $ String length == null || <= 0) {
return 0;
}

$ orginalLength = 0;
$ numberOfBlank = 0;
$ I = 0;
the while ($ String [$ I]! null =) {
++ $ orginalLength;
IF (String $ [$ I] == '') {
++ $ numberOfBlank;
}

++ $ I;
}

$ orginalLength the newLength = $ 2 + $ * numberOfBlank;
IF (the newLength $> length $) {
return 0;
}

$ = $ indexOfOrginal orginalLength; // 14
$indexOfNew = $newLength;
while ($indexOfOrginal >=0 && $indexOfNew >$indexOfOrginal){
if ($string[$indexOfOrginal]==' '){
$string[$indexOfNew--]='0';
$string[$indexOfNew--]='2';
$string[$indexOfNew--]='%';
} else{
$string[$indexOfNew--] = $string[$indexOfOrginal];
}
--$indexOfOrginal;
}

return $string;
}

print_r(ReplaceBlank('we are family! ',30));

方法二:利用PHP内置函数str_replace
function replaceSpace($str)
{
// write code here
return str_replace(" ","%20",$str);
} 


Guess you like

Origin www.cnblogs.com/cyworz/p/11224893.html