Find the news about the regular expression PHP filter editor-php Tutorial

Source: Internet
Author: User
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!

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.