PHP regular expression function learning summary_php tutorial-PHP Tutorial

Source: Internet
Author: User
Tags ereg php regular expression php software
PHP regular expression function learning summary. What about PHP regular expression functions? So what exactly does the PHP regular expression function mean? What is the specific usage? Let's introduce you one by one. What are PHP regular expression functions? So what exactly does the PHP regular expression function mean? What is the specific usage? Let's introduce you one by one. PHP regular expressions are mainly used to process complex strings. The main PHP regular expression functions are as follows:

◆ Ereg ()

◆ Ereg_replace ()

◆ Eregi ()

◆ Eregi_replace ()

◆ Split ()

PHP regular expression function usage induction:

(1) ereg, eregi

This is a regular expression matching function. The former is case-sensitive and the latter is irrelevant.

Usage:

Ereg (regular expression, string, [match part of the array name]); regular expression in PHP3.0 is generally similar to that in grep.

(2) ereg_replace, eregi_replace

These are replacement functions.

Usage:

Ereg_replace (regular expression, replacement string, original string );

The string processing function has a strtr, which is a "translation" function, similar to tr/.../in Perl /.../.../,

Usage:

Strtr (string, "from", "");

For example:

Strtr ("aaabb", "AB", "cd") returns "cccdd ".

(3) split

It is similar to The explode function, but this time it can be used to split the string where a regular expression is matched.

Usage:

Split (regular expression, string, [number of items before removal]);

These functions use regular strings as the first parameter. PHP uses the extended regular string defined by the Posix 1003.2 standard. For a complete description of Posix regular expressions, see the man page under the regex directory in the PHP software package.

PHP regular expression function application example:

 
 
  1. ereg("abc",$string);
  2. /* Returns true if "abc" is
  3. found anywhere in $string. */
  4. ereg("^abc",$string);
  5. /* Returns true if "abc" is
  6. found at the beginning of $string. */
  7. ereg("abc$",$string);
  8. /* Returns true if "abc" is
  9. found at the end of $string. */
  10. eregi("(ozilla.[23]|MSIE.3)",$HTTP_USER_AGENT);
  11. /* Returns true if client browser
  12. is Netscape 2, 3 or MSIE 3. */
  13. ereg("([[:alnum:]]+) ([[:alnum:]]+)
  14. ([[:alnum:]]+)",$string,$regs);
  15. /* Places three space separated words
  16. into $regs[1], $regs[2] and $regs[3]. */
  17. ereg_replace("^","",$string);
  18. /* Put a tag at the beginning of $string. */
  19. ereg_replace("$","",$string);
  20. /* Put a tag at the end of $string. */
  21. ereg_replace(" ","",$string);
  22. /* Get rid of any carriage
  23. return characters in $string. */

The related content of PHP regular expression functions will be introduced here, and it is helpful for you to understand and learn PHP regular expression functions.


Why? So what exactly does the PHP regular expression function mean? What is the specific usage? Let's introduce you one by one. PHP regular expression...

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.