PHP: Making an excerpt with regular expression
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('[...]', $conte开发者_高级运维nt_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.
Try capturing the whitespace before [...]
and ignore it with ?:
"/<p>(.*?)(?:\s*)\[...\](.*?)<\/p>/is"
精彩评论