0

I have a post with YouTube video in it. The content is inside $content. Currently I am doing the_content() and this displays full content on my page. The page contains the list of all videos. The problem is that sometimes the full content is too large to display in this list. So I want to limit the text in the content, but then still want show the first video:

I want to

  1. get text: take first 200 characters from the content, but without including video
  2. get video: take just video link and show YouTube video under the text

I am not sure how to archive this. Probably, I would have to use some Regexp and probably I can invent the wheel by my self, but I believe that there should be something already done in WordPress. As I am quite new in WP, I have not found anything yet.

  • 3
    No one will know how to do this unless you show us what you have tried or provide a better explanation. – Wyck Jul 12 '14 at 16:16
  • I have modified my question a bit. I am not sure that this is enough. – renathy Jul 13 '14 at 8:35
2

Assuming your content is in the variable $content you would begin by removing all html tags using wp_strip_all_tags:

$new_content = wp_strip_all_tags( $content );

Then you will want to trim that to 200 characters using substr:

$new_content = substr( $new_content, 0, 200 );

To retrieve the YouTube video, you'll need to use preg_match:

preg_match( '/(<iframe.*?src="(.*?youtube.*?)".*?<\/iframe>)/', $html, $matches );
if ( $matches ) {
  $iframe = $matches[1];
  $url = $matches[2];
}

(I wasn't sure whether you wanted the iframe for embedding, or the src URL, so I've included matching for both.)

  • Btw, were did you take this regexp? And do you have a regexp if post doesn't conteint iframe (just youtube link directly in the post, becaues currently users use this method to insert youtube videos)? – renathy Jul 15 '14 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.