Find the news content (in HTML source code format) read by the regular expression PHP filter editor from website A database and write it into the news Table of website B. The format is not uniform, in addition, there are A lot of redundant code, many of which are copied from the office. you need to filter out the redundant HTML code in website A's news content. The NEWS content is in the $ NEWS field of php. use a regular expression to process this field.
For example
Level 1TitleBold 1
News content
Level 1 title Bold 2
This text is centered
The text is right-aligned.
The text is italic.
Add a link
To process
Level 1 title bold
News content
Section 2 News
Level 1 title Bold 2
This text is centered
The text is right-aligned.
The text is italic.
Add a link
The specific code description is written on a webpage for easy reading.: Http://www.sunmuu.com/help/editorHelp.html
The following is the php connection query code to facilitate testing. for mysql database, the table is editor, and the two fields are ID (INIT) and news (MEDIUMTEXT ):
$ Hosts = "localhost"; $ mysql_db_user = "root"; $ mysql_db_password = "root"; $ mysql_db_database = "test"; $ con = mysqli_connect ($ mysql_db_hostname, $ mysql_db_user, $ mysql_db_password, $ mysql_db_database); mysqli_query ($ con, "set names utf8"); $ SQL = "SELECT * FROM editor"; $ re = mysqli_query ($ con, $ SQL) or die ("An error occurred while reading data ". mysqli_error (); while ($ row = mysqli_fetch_array ($ re) {$ str = $ row ["news"]; echo $ str ;}
Reply to discussion (solution)
// Remove the unallowed tag $ s = strip_tags ($ s ,',,,,'); // Remove the attribute $ s = preg_replace ('/<(p | strong | em) [^>] +?> /I ',' <$1> ', $ s); // img and a handle $ s = preg_replace ('/<(a | img) separately. +? (Href | src) = "([^"] + ?) "[^>] *?> /I ',' <$1 $2 = "$3"> ', $ s); echo $ s;
Try it. you can change it if it doesn't affect the display.
// Remove the unallowed tag $ s = strip_tags ($ s ,',,,,'); // Remove the attribute $ s = preg_replace ('/<(p | strong | em) [^>] +?> /I ',' <$1> ', $ s); // img and a handle $ s = preg_replace ('/<(a | img) separately. +? (Href | src) = "([^"] + ?) "[^>] *?> /I ',' <$1 $2 = "$3"> ', $ s); echo $ s;
Try it. you can change it if it doesn't affect the display.
Thank you very much, jam00 !!
One thing is that the style align attribute in the p tag must be retained. style = "text-align: center;" and style = "text-align: left ;", what is the logical order? Can I give you a prompt?
// Remove the unallowed tag $ s = strip_tags ($ s ,',,,,'); // Remove the attribute $ s = preg_replace ('/<(strong | em) [^>] +?> /I ',' <$1> ', $ s); // img and a handle $ s = preg_replace ('/<(a | img) separately. +? (Href | src) = "([^"] + ?) "[^>] *?> /I ',' <$1 $2 = "$3"> ', $ s); // process p $ s = preg_replace ('/
/I ','
', $ S); echo $ s;
// Remove the unallowed tag $ s = strip_tags ($ s ,',,,,'); // Remove the attribute $ s = preg_replace ('/<(strong | em) [^>] +?> /I ',' <$1> ', $ s); // img and a handle $ s = preg_replace ('/<(a | img) separately. +? (Href | src) = "([^"] + ?) "[^>] *?> /I ',' <$1 $2 = "$3"> ', $ s); // process p $ s = preg_replace ('/
/I ','
', $ S); echo $ s;
Awesome. thank you!