Get the Intro: PHP Paragraph Regular Expression
Recently I was playing around with WordPress' wp_posts table. I wanted to grab the basic information about my posts (ID, title, content, slug) and build a quick summary list of them. One problem I ran into was creating a content "intro." Luckily a quick regular expression allowed me to create the intro.
The PHP
preg_match("/<p>(.*)<\/p>/",$post['post_content'],$matches);
$intro = strip_tags($matches[1]); //removes anchors and other tags from the intro
The above code extracts the first paragraph in a string. Since all of my posts begin with an introductory paragraph, this type of system will work.
One of the web components I've always loved has been Facebook's modal dialog. This "lightbox" isn't like others: no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much." With Facebook's dialog in mind, I've created LightFace: a Facebook lightbox...
My team mate Edna Piranha is not only an awesome hacker; she's also a fantastic philosopher! Communication and online interactions is a subject that has kept her mind busy for a long time, and it has also resulted in a bunch of interesting experimental projects...
Last week I shared my MooTools Typewriter Class with you. It was pretty well received and I got a few feature requests that I've implemented including "backspacing" and character variance delays. I'm not going to explain the old code, so click here...
In my oh-so-humble opinion, Facebook's Modal box is the best modal box around. It's lightweight, subtle, and very stylish. I've taken Facebook's imagery and CSS and combined it with MooTools' awesome functionality to duplicate the effect.
The Imagery
Facebook uses a funky sprite for their modal...
A few things I’d recommend. allow for a CSS style in your regex, make the regex case-insensitive, and have the regex treat the subject as a single line.
Good suggestion Matt!
@Matt, you probably meant:
Otherwise, it would match everything between the first <p> tag and the last </p>
@Jeremy, that’s it! i knew i was forgetting something.
Has one of you ever read the doc ?
The “i” modifier allow paragraph tag name to be lower case or upper case. The “s” modifier allow dot metacharacter to match all characters, including newlines. The “U” modifier make pattern really matching only the first paragraph instead of everything between the first p end the last p. Using the “#” delimiter for your pattern make it more readable when dealing with HTML tags and using single quote to declare the pattern string don’t start PHP automatic search for somes vars to replace.
David, are you doing this from within wordpress via a plugin or something, or outside wordpress and just accessing the database?
If the first one there are better ways of doing it by using the WP functions, like the_excerpt_rss()…
Slight correction to Grouchy Smurf’s regex:
$pattern = “#]*>(.*?)#is”;
What does this code actually do? Is there any way to style the first paragraph by separating it first in PHP, then using CSS?
nice idea, but how do I add the code? how if i just paste it after the_title ? is it works if i do that?
This worked like a champ, but lets say I wanted to get the rest of the content now, minus the intro paragraph? how would I go about doing that? for example. i want to show the intro paragraph using your code, then create a div that would enclose the rest of the actual content that I would be able to toggle through a read more link.
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}
tosomething
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…
Could you please help me to preg match two or more paragraph,I mean all the paragraph from the $post[‘post_content’]