Stripping Tags

Lets say I have a line that looks like this:

<?php if($var['post_content']): ?><?php echo $var['post_content']; ?><?php endif; ?>
  1. How can I strip certain tags within the line above? So for example, when I use this code it shows the picture I have attached to a post and the text. I want only the text. I would also like to strip shortcodes too.

Thanks

For regular HTML and PHP, use [fphp]strip_tags[/fphp]

For removing shortcodes, you’ll likely have to write a regular expression (do you have any examples of the shortcodes you want to remove?)

Hi There,

Yes I did see the strip_tags attribute but unfortunately I am not sure how I would implement it correctly within the line of code I have above. And the shortcode that I would like to remove is a photogallery within the page content. It looks like this for example:
[soliloquy id=“88”].

Basically this is what I have.

  1. A results page (From doing a search. The results page is where I will put this line of code).
  2. When someone clicks on the “get more details” link they go to the actual listing page that has all of the details.

What I am trying to accomplish is to simply have some description text for each result. When I use post_content it includes the photo and my gallery code and i don’t want that to show on my results page. There’s probably a better way of doing this but for now its what I can think of.

This seems to work okay for me:

<?php if($var['post_content']): ?><?php echo strip_tags(preg_replace('/\\[.*?\\]/', '', $var['post_content'])); ?><?php endif; ?>

Cpradio - THAT IS SO BOSS! Thank you!!! I have been trying to figure this out for a few days (im new lol) , looking up what preg_replace means right now :).

One more question, if I wanted to put a character limit (and at the end add something like a trail of dots …) in the post_content is that possible with the code given or would you have to write something different than that? (you don’t have to provide the code if you don’t want to, I may be able to hide it with css).

I really appreciate this, because I will be able to help someone else in a similar situation.

Yeah, you can do it (replace 200 with the number of characters you want shown).

<?php if($var['post_content']): ?><?php echo substr(strip_tags(preg_replace('/\\[.*?\\]/', '', $var['post_content'])), 0, 200); ?>...<?php endif; ?>

Excellent! Thank you so much again, I really appreciate your time and input on this! Will definitely break this down and study it so I can learn more!

If you have any questions as to what it does, just ask. I’ll be glad to explain anything in detail.

Sure, please explain the final line of code! It will definitely help as far as my own troubleshooting when putting something like this together in the future. I know this will help someone else. In trying to figure out this line of code it was always a “sorta close but no cigar” type of deal :rolleyes:.

I’ll explain the entire line broken up into their own parts

The following checks if post_content is available as a key inside $var (which is an array)

<?php if($var['post_content']): ?>

I’m going to do this explanation from the inside out, as that is how it will be processed by PHP
The preg_replace will find ALL […] where there is an opening [ and a closing ] with ANY text in between (There are \ in front of [ and ] because those are special characters to regex and they must be escaped (otherwise they would be considered holding a range of acceptable/unacceptable characters). It will replace all instances of […] found in $var[‘post_content’] with an empty string.

The output of that replacement is then sent to strip_tags which removes ALL HTML and PHP tags from the output of preg_replace (remember preg_replace only removed shortcodes). After removing all tags the output is sent to substr.

substr takes the output of strip_tags and only grabs the first 200 characters. The 0 represents where to start in the string (so we are starting at the beginning of the string, and 200 is how many characters you want to grab AFTER (and including) that starting position. It’s output is then sent to echo which writes the final result to the screen.

<?php echo substr(strip_tags(preg_replace('/\\[.*?\\]/', '', $var['post_content'])), 0, 200); ?>...

Closes the if statement that was opened at the beginning

<?php endif; ?>

Okay, so let’s do an example, let’s say $var[‘post_content’] contains the following value:

[noparse]This is a test that I created for a <a href="sitepoint.com">sitepoint</a> question, it was answered [b][i]quickly[/i][/b]! I need a lot more text to make this over 200 characters so I'm just going to keep typing random words until that limit is passed by so this can be shown correctly.[/noparse]

When it processes through preg_replace, the output of preg_replace will be

This is a test that I created for a <a href="sitepoint.com">sitepoint</a> question, it was answered quickly! I need a lot more text to make this over 200 characters so I'm just going to keep typing random words until that limit is passed by so this can be shown correctly.

That phrase then gets passed into strip_tags, and when strip_tags is done, the phrase will look like

This is a test that I created for a sitepoint question, it was answered quickly! I need a lot more text to make this over 200 characters so I'm just going to keep typing random words until that limit is passed by so this can be shown correctly.

Then substr will receive the above phrase and take the first 200 characters which will be:

This is a test that I created for a sitepoint question, it was answered quickly! I need a lot more text to make this over 200 characters so I'm just going to keep typing random words until that limit

Then echo writes out that phrase and the ‘…’ gets appended because it is behind the echo statement. So your final output

This is a test that I created for a sitepoint question, it was answered quickly! I need a lot more text to make this over 200 characters so I'm just going to keep typing random words until that limit...

Wow. It’s interesting how something that looks so simple does so much! Thanks for taking the time to write this, it was a great read.