The PHP string replacement function can replace multiple keywords at the same time.
- /**
- * String replacement function
- * Edit: bbs.it-home.org
- */
- Function replace ($ string, $ keyArray, $ replacement, $ I ){
- $ Result = '';
- If ($ I <(count ($ keyArray ))){
- $ StrSegArray = explode ($ keyArray [$ I], $ string );
- Foreach ($ strSegArray as $ index => $ strSeg ){
- $ X = $ I + 1;
- If ($ index = (count ($ strSegArray)-1 ))
- $ Result = $ result. replace ($ strSeg, $ keyArray, $ replacement, $ x );
- Else
- $ Result = $ result. replace ($ strSeg, $ keyArray, $ replacement, $ x). $ replacement [$ I];
- }
- Return $ result;
- }
- Else {
- Return $ string;
- }
- }
Code description: $ string = 'The key name array can contain both integer and string types of key names. 12345678 because PHP does not actually distinguish between the index array and the associated array. If no key name is specified for the given value, the current maximum integer index value is used, and the new key name is the value plus one. If the specified key name already has a value, the value will be overwritten. '; Example:
- $ KeyArray = array ('array', 'INTEGER', '123', 'keyname ');
- $ Replacement = array ('AAA', 'bbbbbb', 'cccccc', 'ddd ');
- Echo replace ($ string, $ keyArray, $ replacement, 0 );
|