PHP Paragraph Regular Expression

9th June 2010 - 3 minutes read time

I quite often find the need to extract a section of text from the beginning of a blog post or similar to be used as the excerpt. I normally use a function that will count the number of whole words available and return the string containing those words.

A good alternative to this, although only applicable if the original post is in HTML, is to use a regular expression to extract the contents. The following code will take a string and extract just the first paragraph of text.

$intro = '';
preg_match("/<p.*?>(.*?)<\/p>/is", $string, $matches);
if (isset($matches[1])) {
    $intro = trim(strip_tags($matches[1]));

If the regular expression finds any matches to paragraph tags then it strips out the HTML and trims the string so that the final output doesn't have any formatting or whitespace. The i modifier is used to make the matching case insensitive and the s modifier is used to make the "." match all characters, including new lines. Without the s modifier the result wouldn't return anything if the paragraph text contains a newline character.

This sort of excerpt extraction can be used in systems that store posts as HTML like Wordpress or Drupal.



Hi, I'm in a trouble with php paragraph replace. I've following para

{tab=Sed facilisis consequat libero}Fusce eu ligula purus, eu ultricies nisl. Fusce eu ligula purus, eu ultricies nisl. Curabitur sed leo felis. {/tab}

{tab=Curabitur ultricies sapien}Sed facilisis consequat libero, at tincidunt neque tristique vitae. {/tab}

my aim is to replace {tab=name}something{/tab} to <div class="tab">something</div>

Reg exp. i'm using is: /({tab.+?})(.*\S)({\/tab})/s

Its working fine with single line or para. but not with multiline. Can anybody suggest how to fix it. Thanks in Advance... (Thu, 10/06/2011 - 10:18)

I was searching for php tutorial came across your website thanks for valuable info

hannah (Sun, 11/19/2017 - 12:13)

valuable info thanks for sharing

hannah (Sun, 11/19/2017 - 12:15)

valuable info thanks for sharing

hannah (Sun, 11/19/2017 - 12:19)

I found it usefull with preg_match_all() to extract all texts paragraphs to array (RegExp: "/(.*?)<\/p>|^(.*?)<\/p>$/is"). This way I can display any paragraph I want and I know how many are in text. Thanks ;)

TomZdulski (Thu, 12/28/2017 - 09:33)

I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.

careen joseph (Mon, 03/12/2018 - 09:04)

Add new comment

The content of this field is kept private and will not be shown publicly.