How to match the first and last regular expressions </p>

Source: Internet
Author: User
How to match the first & lt;/p & gt; and the last & lt;/p & gt; such as the following & lt; p & gt; 1111111 & lt; /p & gt; & lt; p & gt; 2222222 & lt;/p & gt; & lt; p & gt; 33333333 & lt;/p & gt; how to match the first regular expression

And the last


For example, the article is as follows:

1111111


2222222


33333333


4444444


55555555


66666666


7777777



Matched output

1111111

In


And the last section

7777777

Of


How to get regular expressions? Share To: and the last one

] For example, the article is as follows:

1111111

2222222

33333333

4444444

55555555

666666... 'data-pics = ''>
------ Solution --------------------
Preg_match_all ('/^

. + <\/P>
------ Solution --------------------

. + <\/P> $/U', $ s, $ m );
------ Solution --------------------

$s =<<< TXT

1111111


2222222


33333333


4444444


55555555


66666666


7777777


TXT;
echo preg_replace('#(

)(.+)(

)#s', '$1aaa$2$3BBB', $s);

1111111

Aaa

2222222


33333333


4444444


55555555


66666666


7777777

BBB


Reference:
Quote: reference:

Quote: reference:

If you only want to match

You don't need regular expressions.
PHP5, no one should use PHP4 now...

$str = '

1111111


2222222


33333333


4444444


55555555


66666666


7777777

';
$fp = stripos($str, '

');
$first = substr($str, $fp, 4);
$ep = stripos($str, '

');
$end = substr($str, $ep, 4);


Wrong, $ ep = strripos ($ str ,'

');
Brother upstairs is the same as I think.

In fact, I want to append some text at the end of the first paragraph, and add some text at the end of the last paragraph.

------ Solution --------------------
Reference:
Quote: reference:

Quote: reference:

If you only want to match

You don't need regular expressions.
PHP5, no one should use PHP4 now...

$str = '

1111111


2222222


33333333


4444444


55555555


66666666


7777777

';
$fp = stripos($str, '

');
$first = substr($str, $fp, 4);
$ep = stripos($str, '

');
$end = substr($str, $ep, 4);


Wrong, $ ep = strripos ($ str ,'

');
Brother upstairs is the same as I think.

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.