Interpreting the array substitution code example for PHP function preg_replace () _php Tutorial

Source: Internet
Author: User
php function Preg_replace () Prototype: Mixed preg_replace (mixed $pattern, mixed $replacement, mixed $subject [, int $limit])

The PHP function Preg_replace () is more powerful than the C function. The first three parameters can be used arrays, the fourth parameter $limit can set the number of substitutions, the default is to replace all. Code 6.7 is an application instance of an array substitution.

php function Preg_replace () code 6.7 array substitution

 
 
  1. < ? PHP
  2. String
  3. $ string = "Name: {name}< br>nemail:
    {email}< br>naddress: {address}< br>n ";
  4. Mode
  5. $ Patterns = Array (
  6. "/{address}/",
  7. "/{name}/",
  8. "/{email}/"
  9. );
  10. Replace string
  11. $ Replacements = Array (
  12. "No.5, Wilson St., New York, U.S.A",
  13. "Thomas Ching",
  14. "Tom@emailaddress.com",
  15. );
  16. Output mode substitution results
  17. Print Preg_replace ($patterns,
    $replacements, $string);
  18. ?>

The output results are as follows.

Name:thomas Ching ",
Email:tom@emailaddress.com
Address:no.5, Wilson St., New York, U.S.A
The pattern modifier "E" can be used in preg_replace regular expressions. The effect is to use the match result as an expression, and it can be re-calculated. For example:

 
 
  1. < ? PHP
  2. $ Html_body =
  3. " < HTML>
  4. < Body>
  5. < H1>TEST< /h1>
  6. My Picture < Img src= "my.gif">
  7. < /body >
  8. < /html > ";
  9. The HTML tags in the output will all be lowercase letters
  10. Echo Preg_replace (
  11. "/(< /?) (w+) (^>]* > )/E ",
  12. "' \1 '. Strtolower (' \2 '). ' \3 ' ",//mode variable here
    \2 will be converted to lowercase characters by strtolower
  13. $html _body);
  14. ?>

The above is the PHP function preg_replace () of the specific use of the introduction, I hope to be helpful to everyone.


http://www.bkjia.com/PHPjc/446246.html www.bkjia.com true http://www.bkjia.com/PHPjc/446246.html techarticle php function Preg_replace () Prototype: Mixed preg_replace (mixed $pattern, mixed $replacement, mixed $subject [, int $limit]) PHP function pre G_replace () is more powerful than C. Its top three ...

  • 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.