正则如何匹配第一个的</p>和最后一个</p>
比如文章如下
1111111
2222222
33333333
4444444
55555555
66666666
7777777
匹配出
1111111
中的和最后一段
7777777
的正则如何弄?
回复讨论(解决方案)
preg_match_all('/^
.+|
.+$/U',$s,$m);
preg_match_all('/^
.+|
.+$/U',$s,$m);
这样匹配到的是整一段了
我要的只是一个
preg_match_all('/^
.+()|
.+()$/U',$s,$m);
$s =<<< TXT<p>1111111</p><p>2222222</p><p>33333333</p><p>4444444</p><p>55555555</p><p>66666666</p><p>7777777</p>TXT;preg_match('#(</p>).+(</p>)#s', $s, $r);print_r($r);
(
[0] =>
2222222
33333333
4444444
55555555
66666666
7777777
[1] =>
[2] =>
)
如果只是想要匹配
的话,可以不用正则吧。PHP5,现在应该没有谁用PHP4吧。。。
$str = '<p>1111111</p><p>2222222</p><p>33333333</p><p>4444444</p><p>55555555</p><p>66666666</p><p>7777777</p>';$fp = stripos($str, '</p>');$first = substr($str, $fp, 4);$ep = stripos($str, '</p>');$end = substr($str, $ep, 4);
别用正则了
<?php $s='<p>1111111</p><p>2222222</p><p>33333333</p><p>4444444</p><p>55555555</p><p>66666666</p><p>7777777</p>';print strlen($s);print stripos($s,'</p>');print strripos($s,'</p>');?>运行结果:11310109
如果只是想要匹配
的话,可以不用正则吧。PHP5,现在应该没有谁用PHP4吧。。。
$str = '<p>1111111</p><p>2222222</p><p>33333333</p><p>4444444</p><p>55555555</p><p>66666666</p><p>7777777</p>';$fp = stripos($str, '</p>');$first = substr($str, $fp, 4);$ep = stripos($str, '</p>');$end = substr($str, $ep, 4);
写错了,$ep = strripos($str, '');
楼上兄弟和我想一样了
如果只是想要匹配
PHP5,现在应该没有谁用PHP4吧。。。
$str = '<p>1111111</p><p>2222222</p><p>33333333</p><p>4444444</p><p>55555555</p><p>66666666</p><p>7777777</p>';$fp = stripos($str, '</p>');$first = substr($str, $fp, 4);$ep = stripos($str, '</p>');$end = substr($str, $ep, 4);
写错了,$ep = strripos($str, '');
楼上兄弟和我想一样了
其实我是要第一段的后面追加一些文字、和最后一段也追加一些文字
$s =<<< TXT<p>1111111</p><p>2222222</p><p>33333333</p><p>4444444</p><p>55555555</p><p>66666666</p><p>7777777</p>TXT;echo preg_replace('#(</p>)(.+)(</p>)#s', '$1aaa$2$3BBB', $s);
1111111
aaa2222222
33333333
4444444
55555555
66666666
7777777
BBB如果只是想要匹配 的话,可以不用正则吧。
PHP5,现在应该没有谁用PHP4吧。。。
$str = '<p>1111111</p><p>2222222</p><p>33333333</p><p>4444444</p><p>55555555</p><p>66666666</p><p>7777777</p>';$fp = stripos($str, '</p>');$first = substr($str, $fp, 4);$ep = stripos($str, '</p>');$end = substr($str, $ep, 4);
写错了,$ep = strripos($str, '');
楼上兄弟和我想一样了
其实我是要第一段的后面追加一些文字、和最后一段也追加一些文字
如果只是想要匹配
PHP5,现在应该没有谁用PHP4吧。。。
$str = '<p>1111111</p><p>2222222</p><p>33333333</p><p>4444444</p><p>55555555</p><p>66666666</p><p>7777777</p>';$fp = stripos($str, '</p>');$first = substr($str, $fp, 4);$ep = stripos($str, '</p>');$end = substr($str, $ep, 4);
写错了,$ep = strripos($str, '');
楼上兄弟和我想一样了
其实我是要第一段的后面追加一些文字、和最后一段也追加一些文字
也可以使用这种啊,得出了两个''的位置,在把字符切一下再加上你要加的文字。
不过如果字符串本身很大的话,这个效率不怎么样。
如果只是想要匹配
PHP5,现在应该没有谁用PHP4吧。。。
$str = '<p>1111111</p><p>2222222</p><p>33333333</p><p>4444444</p><p>55555555</p><p>66666666</p><p>7777777</p>';$fp = stripos($str, '</p>');$first = substr($str, $fp, 4);$ep = stripos($str, '</p>');$end = substr($str, $ep, 4);
写错了,$ep = strripos($str, '');
楼上兄弟和我想一样了
其实我是要第一段的后面追加一些文字、和最后一段也追加一些文字
也可以使用这种啊,得出了两个''的位置,在把字符切一下再加上你要加的文字。
不过如果字符串本身很大的话,这个效率不怎么样。
不用切,找出位置直接替换就可以
<?php $s='<p>1111111</p><p>2222222</p><p>33333333</p><p>4444444</p><p>55555555</p><p>66666666</p><p>7777777</p>';print strlen($s);$find = '</p>';$find_len = 4; //</p> 长度$s = substr_replace($s, $find."第一个p", stripos($s,'</p>'), $find_len);$s = substr_replace($s, $find."最后一个p", strripos($s,'</p>'), $find_len);print $s;
如果只是想要匹配
PHP5,现在应该没有谁用PHP4吧。。。
$str = '<p>1111111</p><p>2222222</p><p>33333333</p><p>4444444</p><p>55555555</p><p>66666666</p><p>7777777</p>';$fp = stripos($str, '</p>');$first = substr($str, $fp, 4);$ep = stripos($str, '</p>');$end = substr($str, $ep, 4);
写错了,$ep = strripos($str, '');
楼上兄弟和我想一样了
其实我是要第一段的后面追加一些文字、和最后一段也追加一些文字
也可以使用这种啊,得出了两个''的位置,在把字符切一下再加上你要加的文字。
不过如果字符串本身很大的话,这个效率不怎么样。
不用切,找出位置直接替换就可以
<?php $s='<p>1111111</p><p>2222222</p><p>33333333</p><p>4444444</p><p>55555555</p><p>66666666</p><p>7777777</p>';print strlen($s);$find = '</p>';$find_len = 4; //</p> 长度$s = substr_replace($s, $find."第一个p", stripos($s,'</p>'), $find_len);$s = substr_replace($s, $find."最后一个p", strripos($s,'</p>'), $find_len);print $s;
这个可以

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Alipay PHP...
