Simple regular expression substitution related issues

Source: Internet
Author: User
Simple Regular expression substitution problem
See a topic from somewhere else:

In the HTML language, the meta tag of the header of the page can be used to output the encoded format of the file, and the following is a standard META statement

Use the PHP language to write a function that changes the value of the charset part of a standard HTML page like a meta tag to Big5
Please note:
1. The full HTML page needs to be processed, i.e. not only this meta statement
2. Ignore case
3. ' and ' can be interchanged in this place
4. The quotation marks on both sides of ' content-type ' are negligible, but ' text/html; CHARSET=GBK ' on both sides of the No
5. Pay attention to handling extra spaces

The first thought is the regular expression substitution, as follows:
PHP Code
  
   $content = "AB
  
    B";//
  
    //preg_match ('/]+) charset=[a-za-z-0-9]+ in the simulated HTML code
  
   
    
   ([\ ' "]) >/i ', $content, $matches)//var_dump ($matches);//This statement can be matched to preg_replace ('/
   
    
     
    ]+) charset=[ a-za-z-0-9]+ ([\ ' "]) >/i ',"
    
     
      
     ", $content); Echo $content;  Output ABB, that is, the replacement is unsuccessful, replace the matched
    part with the empty
     
   
    
  
   


The above is replaced, where there is a problem, seek guidance!

------Solution--------------------
No problem
PHP code
 $content = Preg_replace ('/
   ]+) charset=[a- za-z-0-9]+ ([\ ' "]) >/i '," 
   
     ", $content), Echo ' 
    
 '; e Cho $content; 
  • 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.