Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDylan Hobbs
9,279 PointsHow to remove full HTML entities/blocks with PHP
I am trying to remove html <video> and <iframe> tags from my wordpress theme so that none of the contents within the block will be shown.
Meaning, I don't want to just remove the tags but all of the data inbetween the tags. I am guessing this can be done with some string manipulation, but I am not sure how the syntax would work or how to write it out.
here is my code
function custom_trim_excerpt($text) {
global $post;
if ('' == $text) {
$text = get_the_content('');
$text = apply_filters('the_content', $text);
$text = str_replace('\]\]\>', ']]>', $text);
$text = preg_replace('@<script[^>]*?>.*?</script>@si', '', $text);
$text = strip_tags($text, '<p>,<a>,<em>,<blockquote>,<iframe>');
$excerpt_length = 15;
$type = get_post($post);
if($type->post_type == "post" ) {
$excerpt_length =7;
}
$words = explode(' ', $text, $excerpt_length + 1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, '...');
$text = implode(' ', $words);
}
}
return $text;
}
I haven't put any of the code in here to attempt it, this is just the block I would like to insert it into. So that any of the text is stripped of these tags and the content in between them.
Any help/advice is greatly appreciated! Thanks!
1 Answer
Roberto Alicata
Courses Plus Student 39,959 PointsWhen you are working with HTML (or XML), it's better to choose one of these libraries/extensions http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php