PHP:使用正则表达式进行摘录
我正在尝试通过匹配此 - [...] 来摘录我的内容
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试捕获
[...]
之前的空格并使用?:
忽略它Try capturing the whitespace before
[...]
and ignore it with?: