The PHP string replacement function can replace multiple keywords at the same time.

Source: Internet
Author: User
The PHP string replacement function can replace multiple keywords at the same time.

  1. /**
  2. * String replacement function
  3. * Edit: bbs.it-home.org
  4. */
  5. Function replace ($ string, $ keyArray, $ replacement, $ I ){
  6. $ Result = '';
  7. If ($ I <(count ($ keyArray ))){
  8. $ StrSegArray = explode ($ keyArray [$ I], $ string );
  9. Foreach ($ strSegArray as $ index => $ strSeg ){
  10. $ X = $ I + 1;
  11. If ($ index = (count ($ strSegArray)-1 ))
  12. $ Result = $ result. replace ($ strSeg, $ keyArray, $ replacement, $ x );
  13. Else
  14. $ Result = $ result. replace ($ strSeg, $ keyArray, $ replacement, $ x). $ replacement [$ I];
  15. }
  16. Return $ result;
  17. }
  18. Else {
  19. Return $ string;
  20. }
  21. }

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:

  1. $ KeyArray = array ('array', 'INTEGER', '123', 'keyname ');
  2. $ Replacement = array ('AAA', 'bbbbbb', 'cccccc', 'ddd ');
  3. Echo replace ($ string, $ keyArray, $ replacement, 0 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.