|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
通过这段时间的学习实践,对软件开发有了更多新的认识,不在局限于之前的片面性。当然,现在所学到的工具其实并不多,离当一个真正的程序员,还有很大的差距。str_replace
(PHP4,PHP5)
str_replace―Replacealloccurrencesofthesearchstringwiththereplacementstring
Description
mixedstr_replace(mixed$search,mixed$replace,mixed$subject[,int&$count])Thisfunctionreturnsastringoranarraywithalloccurrencesofsearchinsubjectreplacedwiththegivenreplacevalue.
Ifyoudontneedfancyreplacingrules(likeregularexpressions),youshouldalwaysusethisfunctioninsteadofpreg_replace().
Parameters
Ifsearchandreplacearearrays,thenstr_replace()takesavaluefromeacharrayandusesthemtosearchandreplaceonsubject.Ifreplacehasfewervaluesthansearch,thenanemptystringisusedfortherestofreplacementvalues.Ifsearchisanarrayandreplaceisastring,thenthisreplacementstringisusedforeveryvalueofsearch.Theconversewouldnotmakesense,though.
Ifsearchorreplacearearrays,theirelementsareprocessedfirsttolast.
searchThevaluebeingsearchedfor,otherwiseknownastheneedle.Anarraymaybeusedtodesignatemultipleneedles.
replaceThereplacementvaluethatreplacesfoundsearchvalues.Anarraymaybeusedtodesignatemultiplereplacements.
subjectThestringorarraybeingsearchedandreplacedon,otherwiseknownasthehaystack.
Ifsubjectisanarray,thenthesearchandreplaceisperformedwitheveryentryofsubject,andthereturnvalueisanarrayaswell.
countIfpassed,thiswillbesettothenumberofreplacementsperformed.
ReturnValues
Thisfunctionreturnsastringoranarraywiththereplacedvalues.
Changelog
VersionDescription5.0.0Thecountparameterwasadded.4.3.3Thebehaviourofthisfunctionchanged.Inolderversionsabugexistedwhenusingarraysasbothsearchandreplaceparameterswhichcausedemptysearchindexestobeskippedwithoutadvancingtheinternalpointeronthereplacearray.ThishasbeencorrectedinPHP4.3.3,anyscriptswhichreliedonthisbugshouldremoveemptysearchvaluespriortocallingthisfunctioninordertomimictheoriginalbehavior.4.0.5Mostparameterscannowbeanarray.Examples
Example#1Basicstr_replace()examples |
|