PHP replaces spaces, line breaks, Chinese commas, and so on with the regular expression of English commas

Source: Internet
Author: User
This article describes how to replace spaces, line breaks, Chinese commas, and so on with regular expressions of English commas. For more information, see

This article describes how to replace spaces, line breaks, Chinese commas, and so on with regular expressions of English commas. For more information, see

In the process of development, you will often encounter a need to provide an input box for others, and then others enter some ID or keyword, such as the wordpress background tag input box:

This is just a comma in English. What if someone accidentally enters a comma in Chinese? What about decimal point?

So I wrote an expression with a regular expression and replaced the line break with a space with a comma.

Replace the submitted id with a space newline character with a comma, and then use the explode function to switch to an array.

The Code is as follows:


$ Ids = $ _ POST ["ID"];
$ Id = preg_replace ("/(\ n) | (\ s) | (\ t) | (\ ') | (,)/",', ', $ ids );
$ Topicids = explode (",", $ tids );

This adds a decimal point filter.

The Code is as follows:


$ Ids = preg_replace ("/(\ n) | (\ s) | (\ t) | (\ ') | (,) | (\.) /", ',', $ ids );

OK. After such processing, there will be no problem.

Tip: in fact, it is a better choice for xiaobian to handle this problem before submitting JavaScript code.

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.