How PHP ereg_replace () functions work

Source: Internet
Author: User

We are learningPHP ereg_replace () function prototype: string ereg_replace (string $ pattern, string $ replacement, string $ string)

String eregi_replace (string $ pattern, string $ replacement, string $ string)

PHP ereg_replace () function c, and replace the matched result with $ replacement. When $ pattern contains a pattern unit (or child mode), the position in $ replacement, such as "1" or "$1", will be replaced by the content matched by these child modes in sequence. "" Or "$0" indicates the content of the entire matching string. Note that the backslash in double quotation marks is used as an escape character, so the format of "\ 0" and "\ 1" must be used.

Eregi_replace () and PHP ereg_replace () functions are the same, except that the former is case-insensitive. Code 6.6 is an application instance of this function. This Code demonstrates how to clean up the source code of the program.

PHP ereg_replace () function code 6.6 source code cleaning

 
 
  1. <? Php
  2. $ Lines = file ('source. php'); // read the file into the array.
  3. For ($ I = 0; $ I <count ($ lines); $ I ++)
  4. {
  5. // Remove the comment starting with "\" or "#" at the end of the line
  6. $ Lines [$ I] = eregi_replace ("(// | #). * $", "", $ lines [$ I]);
  7. // Eliminate the white space at the end of the row
  8. $ Lines [$ I] = eregi_replace ("[nrtvf] * $", "rn", $ lines [$ I]);
  9. }
  10. // Output to the page after sorting
  11. Echo htmlspecialchars (join ("", $ lines ));
  12. ?>

The above describes the usage of the PHP ereg_replace () function.


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.