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;