Simple regular expression replacement-php Tutorial

Source: Internet
Author: User
A simple regular expression replacement problem can be found elsewhere: in HTML language, meta tags in the page header can be used to output the encoding format of the file. The following is a standard meta statement & lt; METAhttp-equivContent-Typecontenttexthtml; charsetgbk & gt; use the PHP language to write a function to replace a simple regular expression on a standard HTML page.
See a question from other places:

In HTML, the meta tag in the page header can be used to output the file encoding format. The following is a standard meta statement.

Use the PHP language to write a function and change the charset part of a standard HTML page similar to meta tag to big5.
Note:
1. you need to process the complete html page, that is, not only this meta statement
2. case insensitive
3. 'and "are interchangeable here
4. the quotation marks on both sides of 'content-type' can be ignored, but the quotation marks on both sides of 'text/html; charset = gbk' cannot be ignored.
5. handle extra spaces

The first thing that comes to mind is regular expression replacement, as shown below:
PHP code
  $ Content = "AB
  B "; // simulate
  // Preg_match ('/
  
   
] +) Charset = [a-zA-Z-0-9] + ([\ '"])>/I', $ content, $ matches); // var_dump ($ matches ); // This statement can match preg_replace ('/
   
    
] +) Charset = [a-zA-Z-0-9] + ([\ '"])>/I ',"
    
     
", $ Content); echo $ content; // output abb, that is, the replacement is unsuccessful. replace the matching part with null.
    
   
  


If there is any problem with the above regular expression replacement, please advise!

------ Solution --------------------
No problem
PHP code
$content = preg_replace('/
  
   ]+)charset=[a-zA-Z-0-9]+([\'"])>/i',"
   
    ",$content);echo '
    
';echo $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.