A simple example of PHP regular matching and substitution

Source: Internet
Author: User

A simple example of PHP regular matching and substitution, with a match to get an example of a rent-plus font and a matching replacement hyperlink.

1. Find the contents of matching <b> and </b> tags:

<?php$str = "Name: <b>PHP</b> <br> Title: <b>programming language</b>";p Reg_match_ All ("/<b> (. *) <\/b>/u", $str, $arr);p rint_r ($arr [0]);? >

The execution results are as follows:

Array (    [0] = <b>PHP</b>    [1] = <b>programming language</b>)

Matches the "<b></b>" and the contents inside, and the special character needs to be escaped with a backslash before it is added.

2. Match Remove Hyperlink

$str = Preg_replace ("/<a[^>]*>|<\/a>/i", "", $str);

Match hyperlink tag A, ' | ' Should be or means, [^>]* means any character that is not >, or ends with >.

A simple example of PHP regular matching and substitution

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.