I have some code that outputs a title and a description for a list of posts. I am trying to limit the length of text for both. The title is $story->title, and outputs just fine. However the description, represented by $story->excerpt in the code, contains html tags in the database. The limit_text function seems to strip these tags from the text. I think I need to limit the characters differently or need a function to allow those tags to work.
I have tried some functions that allows the tags to be seen but not function properly. But I am new to php in general so I don't know many functions.
- <?php
- foreach($stories as $story) {
- echo '<h2><a href="'.BASE_URL.'/'.$story->slug.'">'.limit_text($story->title, 80).'</a></h2>';
- if(!empty($story->excerpt)) {
- echo '<p>'.limit_text($story->excerpt, 150).'</p>';
- } else {
- echo limit_text($story->body, 150);
- }
- }
- ?>
I found the function for limit_text
- function limit_text($string, $limit = 140) {
- $string = preg_replace('/<figcaption>.*?<\/figcaption>/','',$string);
- $string = preg_replace('/<div class=\"wp_image_caption\">.*?<\/div>/','',$string);
- $string = str_replace(' ','',$string);
-
- $string = substr($string, strpos($string, "<p><strong>"));
- $string = strip_tags($string);
- $string = substr($string, 0, $limit);
- $string = $string.'...';
- return $string;
- }