Change Excerpt Length to First Paragraph of the_content

Hi,

How can you change the length of the excerpt to be the first paragraph of the post content?

The code below works if you want to have the first sentence of the content, but how about the first paragraph?

$strings = preg_split('/(\.|!|\?)\s/', strip_tags($post->post_content), 2, PREG_SPLIT_DELIM_CAPTURE);
echo apply_filters('the_content', $strings[0] .  $strings[1]);

Open in new window



Thanks :)
AccurateDesignAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

COBOLdinosaurCommented:
The raw string should contain a linefeed at the end of the paragraph, so you should be able to find that ... value is hex 0a

Cd&
0
AccurateDesignAuthor Commented:
I am not really familiar with regular expressions, could you provide an example?
0
COBOLdinosaurCommented:
You are already using one of the regular expression functions.  This is the manual page for it with links to the rest of the function:

http://www.php.net/manual/en/function.preg-split.php

And if you need some help with the syntax then this reference should give it to you for this and future regex expressions: http://www.regular-expressions.info/reference.html

Cd&
0
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Ray PaseurCommented:
Here is what your regular expression is doing now.  It's looking for a period (full stop), a question mark, or an exclamation mark followed by a whitespace character.

 PHP has some more information available here:
http://php.net/manual/en/reference.pcre.pattern.syntax.php

This is a $2 investment that is worth it:
http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/

$rgx 
= '/'          // REGULAR EXPRESSION DELIMITER
. '('          // START OF CAPTURE GROUP
. '\.'         // ESCAPED DOT (PERIOD)
. '|'          // OR
. '!'          // EXCLAMATION MARK
. '|'          // OR
. '\?'         // ESCAPED QUESTION MARK
. ')'          // END OF CAPTURE GROUP
. '\s'         // WHITE SPACE (BLANK, NEWLINE, TAB, ETC)
. '/'          // REGULAR EXPRESSION DELIMITER

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AccurateDesignAuthor Commented:
I tried this, but it is not working. What I am trying to do is find the first paragraph and echo only that one.

$strings = preg_split('/(\</p>|!|\?)\s/', strip_tags($post->post_content), 2, PREG_SPLIT_DELIM_CAPTURE);
echo apply_filters('the_content', $strings[0] .  $strings[1]);

Open in new window

0
Ray PaseurCommented:
That  makes sense that it would not work.  It's looking for a <p> tag, but the strip_tags() function is being applied to the input.

You might want to use var_dump() to print out the contents of $post->post_content and look for the appropriate information.  If the <p> tag is a useful signal, it can be added to the strip_tags() function call as one of the "allowable tags."
0
AccurateDesignAuthor Commented:
Can you show me an example?

I am a noob to the PHP world and the code in the original question was taken from another forum :P
0
Ray PaseurCommented:
I don't have any test data to work with, so this is just a guess.  Please see the notes and warnings on the documentation.
http://php.net/manual/en/function.strip-tags.php#88991

$strings = preg_split('/(\</p>|!|\?)\s/', strip_tags($post->post_content, '</p>'), 2, PREG_SPLIT_DELIM_CAPTURE);
echo apply_filters('the_content', $strings[0] .  $strings[1]);

Open in new window

0
AccurateDesignAuthor Commented:
Really close to making it work.

The code below works fine without the IF statement and what I am trying to do now is to add the filter on the blog page (page-blog.php) and not in the blog posts (single.php). But for some reason it is not working.

Any help would be appreciated :)


function smart_excerpt($content){

	if (is_page('blog') && !is_single()) {	
		preg_match('%(<p[^>]*>.*?</p>)%i', $content, $paragraph);
		return $paragraph[1];
	}
}

add_filter('the_content', 'smart_excerpt');

Open in new window

0
Ray PaseurCommented:
"Not working" doesn't tell us enough to help you.  Please take a moment to read about the SSCCE, then use that guidance to set up the demonstration that shows us the input data and the output you want.  Thanks, ~Ray
0
Ray PaseurCommented:
@AccurateDesign: Please see the grading guidelines here:
http://support.experts-exchange.com/customer/portal/articles/481419

Please explain why you closed this question with the worst possible grade anyone can give at EE, without responding to our requests for additional information.  It's not possible to help you if you will not engage in the dialog!

Standing by, ~Ray
0
AccurateDesignAuthor Commented:
Very simple, when I ask for help and for an example, you gave a link to a guide telling me to go learn it yourself kind of thing and if I would understand that guide, I wouldn't had ask a question on EE.

Sorry to give the worst grade, but I had to do a lot of extra work to figure out the solution and the regex. Now that I think about it probably a B might be better.
0
Ray PaseurCommented:
What would really be better would be for you to create the SSCCE and post that so we can reproduce the problem.  We don' t know all of the things you know or don't know, and the whole concept of isolating the issues will help us see where you need help and what you already understand.  Are you an experienced PHP programmer?  Do you understand regular expressions?  Did you copy some code you found on the internet?  Did you install it without understanding what the code actually did?  The answers to these questions are the sort of "deeper background" we need if we are going to be able to read the question as you understand it, and give you the best answer.

And please, if we tell you to read a note or article, understand that we are not posting that to put you off or waste your time.  Almost every question asked at EE has been asked before, and in many cases there are accurate, well-explained answers published somewhere (often in the user-contributed notes of the PHP online manual, required reading if you're going to manipulate PHP code).  It does no good to plagiarize those if we can show you the link.  And if you're not reading the online manual, you might want to start doing that, or at least consider hiring a professional programmer who does read the online manual!

Ultimately this is your question, and you have a responsibility to the community to (1) frame it accurately, and to (2) comply with the grading guidelines.  If you do those things, you will be amazed how helpful the EE community can be.
0
AccurateDesignAuthor Commented:
Thanks, I will keep that in mind next time
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
WordPress

From novice to tech pro — start learning today.