PHP:使用正则表达式进行摘录

发布于 2024-10-13 01:59:45 字数 1140 浏览 3 评论 0原文

我正在尝试通过匹配此 - [...] 来摘录我的内容

$content_filtered = preg_replace("/<p>(.*?)\[...\](.*?)<\/p>/is", "<p>$1...</p>[...]<p>$2</p>", $item['pg_content_1']);

,然后分解此 [...]

$array_content = explode('[...]', $content_filtered);

以生成摘录,

$content_current = $array_content[0]; 

当我像这样使用 [...] 时,它可以正常工作,

<p>Lorem Ipsum is simply dummy text of[...] the printing and typesetting industry.</p>

它会产生类似这样的东西,

Lorem Ipsum is simply dummy text of...

但如果我在 [...] 之前放置一个空格,

<p>Lorem Ipsum is simply dummy text of [...] the printing and typesetting industry.</p>

那么摘录中的点将有一个空格,

Lorem Ipsum is simply dummy text of ...

我怎样才能摆脱那个空格?

我想我必须在下面这一行中使用正则表达式,但我不知道如何!

preg_replace("/<p>(.*?)\[...\](.*?)<\/p>/is", "<p>$1...</p>[...]<p>$2</p>", $item['pg_content_1']);

如果您有任何想法,请告诉我!

谢谢。

I am trying to make an excerpt of my content by matching this - [...]

$content_filtered = preg_replace("/<p>(.*?)\[...\](.*?)<\/p>/is", "<p>$1...</p>[...]<p>$2</p>", $item['pg_content_1']);

and then explode this [...]

$array_content = explode('[...]', $content_filtered);

to produce the excerpt,

$content_current = $array_content[0]; 

it works fine with when I use [...] like this,

<p>Lorem Ipsum is simply dummy text of[...] the printing and typesetting industry.</p>

it will produce something like this,

Lorem Ipsum is simply dummy text of...

but if I put a white space before the [...],

<p>Lorem Ipsum is simply dummy text of [...] the printing and typesetting industry.</p>

then the dots will have a white space in the excerpt,

Lorem Ipsum is simply dummy text of ...

how can I get rid of that white space?

I think I must to something with the regular expression in this line below but I don't know how!

preg_replace("/<p>(.*?)\[...\](.*?)<\/p>/is", "<p>$1...</p>[...]<p>$2</p>", $item['pg_content_1']);

Please let me know if you have any ideas!

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱影子 2024-10-20 01:59:45

尝试捕获 [...] 之前的空格并使用 ?: 忽略它

"/<p>(.*?)(?:\s*)\[...\](.*?)<\/p>/is"

Try capturing the whitespace before [...] and ignore it with ?:

"/<p>(.*?)(?:\s*)\[...\](.*?)<\/p>/is"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文