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

Source: Internet
Author: User
such as the article below

1111111


2222222


33333333


4444444


55555555


66666666


7777777



Match out

1111111

In the


And the last paragraph

7777777

Of


How do you get it?


Reply to discussion (solution)

Preg_match_all ('/^

. +<\/p>|

. +<\/p>$/u ', $s, $m);

Preg_match_all ('/^

. +<\/p>|

. +<\/p>$/u ', $s, $m);
This matches the whole paragraph.
All I want is a

Preg_match_all ('/^

. + (<\/p>) |

. + (<\/p>) $/u ', $s, $m);

$s =<<< TXT

1111111

2222222

33333333

4444444

55555555

66666666

7777777

Txt;preg_match (' # ' (

).+(

) #s ', $s, $r);p Rint_r ($r);

Array
(
[0] = =


2222222


33333333


4444444


55555555


66666666


7777777


[1] = =


[2] = =


)

If you just want to match

, you can use the regular.
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);

Don't use the regular.

 
  1111111

2222222

33333333

4444444

55555555

66666666

7777777

';p rint strlen ($s);p rint stripos ($s, '

');p rint strripos ($s, '

');? > Running results: 11310109

If you just want to match

, you can use the regular.
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);



Wrongly written, $ep = Strripos ($str, '

');
The upstairs brothers and I want to be the same.


If you just want to match

, you can use the regular.
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);



Wrongly written, $ep = Strripos ($str, '

');
The upstairs brothers and I want to be the same.
Actually, I want the first paragraph. Append some text, and the last paragraph to append some text

$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




If you just want to match

, you can use the regular.
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);



Wrongly written, $ep = Strripos ($str, '

');
The upstairs brothers and I want to be the same.
Actually, I want the first paragraph. Append some text, and the last paragraph to append some text



If you just want to match

, you can use the regular.
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);



Wrongly written, $ep = Strripos ($str, '

');
The upstairs brothers and I want to be the same.
Actually, I want the first paragraph. Append some text, and the last paragraph to append some text
can also use this Ah, came to two '

' Position, segmentation the word and add the text you want to add.
But if the string itself is large, this is not very efficient.




If you just want to match

, you can use the regular.
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);



Wrongly written, $ep = Strripos ($str, '

');
The upstairs brothers and I want to be the same.
Actually, I want the first paragraph. Append some text, and the last paragraph to append some text
can also use this Ah, came to two '

' Position, segmentation the word and add the text you want to add.
But if the string itself is large, this is not very efficient.

No need to cut, find the location and replace it directly.
 
  1111111

2222222

33333333

4444444

55555555

66666666

7777777

';p rint strlen ($s); $find = '

'; $find _len = 4; //

Length $s = substr_replace ($s, $find. " The first P ", Stripos ($s, '

'), $find _len); $s = substr_replace ($s, $find. " Last P ", Strripos ($s, '

'), $find _len);p rint $s;





If you just want to match

, you can use the regular.
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);



Wrongly written, $ep = Strripos ($str, '

');
The upstairs brothers and I want to be the same.
Actually, I want the first paragraph. Append some text, and the last paragraph to append some text
can also use this Ah, came to two '

' Position, segmentation the word and add the text you want to add.
But if the string itself is large, this is not very efficient.

No need to cut, find the location and replace it directly.
 
  1111111

2222222

33333333

4444444

55555555

66666666

7777777

';p rint strlen ($s); $find = '

'; $find _len = 4; //

Length $s = substr_replace ($s, $find. " The first P ", Stripos ($s, '

'), $find _len); $s = substr_replace ($s, $find. " Last P ", Strripos ($s, '

'), $find _len);p rint $s;


This can
  • Related Article

    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.