/ **
*各文字列にスペースを置き換える「%20」
*
* @paramの$文字列文字列
* @paramの$長文字列の長さ(置換後の文字列の長さよりも大きくなければなりません)
* /
関数ReplaceBlank($文字列、$長){
($ $文字列長== NULL || <= 0){IF
戻り0;
}
$ orginalLength = 0;
$ numberOfBlank = 0;
$ I = 0;
ながら($文字列[$ I]!ヌル=){
++ $ orginalLength;
IF(文字列$ [$ I] == ''){
++ $ numberOfBlank;
}
++ $ I;
}
$ orginalLength newLength = $ 2 + $ * numberOfBlank;
IF(newLength $>長さ$){
戻り0;
}
$ = $ indexOfOrginal orginalLength; // 14
$ indexOfNew = $ newLength。
一方、($ indexOfOrginal> = 0 && $ indexOfNew> $ indexOfOrginal){
IF($文字列[$ indexOfOrginal] ==」 '){
$列[$ indexOfNew - ] =' 0' ;
$文字列[$ indexOfNew - ] = '2';
$文字列[$ indexOfNew - ] = '%';
}他{
$列[$ indexOfNew--] = $列[$ indexOfOrginal]。
}
- $ indexOfOrginal。
}
$文字列を返します。
}
しますprint_r(ReplaceBlank( '私たちは家族です!'、30));
方法二:利用PHP内置函数str_replace
関数replaceSpace($ strの)
{
//ここに書き込みコードが
str_replace(」」、 "%20"、$ strを)返します。
}