February 1, 201115 yr I want to use part of a blog post as the introduction to the post and then add a "read more" link at the end, but I couldn't find a way to round up the text either at the end of a word or sentence. The code below runs into an empty infinite loop. <?php $num_of_words = 400; while (! substr($article, $num_of_words, 1) != "") $num_of_words++; ?> Any help please? Thanks
February 1, 201115 yr I want to use part of a blog post as the introduction to the post and then add a "read more" link at the end, but I couldn't find a way to round up the text either at the end of a word or sentence. The code below runs into an empty infinite loop. <?php $num_of_words = 400; while (! substr($article, $num_of_words, 1) != "") $num_of_words++; ?> Any help please? Thanks idk if this is what u mean but u could use strlen like if(strlen($post) > 30){ $post = substr($post,0,30).'...'; } That will limit the post and only show up to 30 characters of that post adding ... on the end to show that there is more, then u could add a readmore link at the end as well
February 1, 201115 yr This will work: <?php function truncateContent ($content, $showWords = false){ if (empty($content)) return 'No content provided for function truncateContent.'; $words = preg_split("/ /", $content); $i = 0; while ($i < count($words)){ if ($i >= $showWords && $showWords !== false) break; $snippet .= "{$words[$i]} "; $i++; } return substr($snippet, 0, -1); } ?> Use it like so: <?php echo truncateContent($article, 400) ?> Change 400 to whatever number of words you want to show, or leave it blank to show all. Edited February 1, 201115 yr by andyl
February 1, 201115 yr This will work: <?php function truncateContent ($content, $showWords = false){ if (empty($content)) return 'No content provided for function truncateContent.'; $words = preg_split("/ /", $content); $i = 0; while ($i < count($words)){ if ($i >= $showWords && $showWords !== false) break; $snippet .= "{$words[$i]} "; $i++; } return substr($snippet, 0, -1); } ?> Use it like so: <?php echo truncateContent($article, 400) ?> Change 400 to whatever number of words you want to show, or leave it blank to show all. This is actually quite a nice function i may use it for a project
February 1, 201115 yr This is actually quite a nice function i may use it for a project Oh noes, I should've put a patent on it! I joke, please go ahead and use it as you like
February 1, 201115 yr Oh noes, I should've put a patent on it! I joke, please go ahead and use it as you like lol
February 1, 201115 yr This isn't counting words, it counts characters, I prefer to count characters as it gives you a similar intro size for each article, and its less memory/cpu intensive. <?php function intro($content) { $pos = strpos($content, '.', 400); return substr($content, 0, $pos+1); } $content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent quis urna ut mi mattis tristique. Curabitur placerat blandit aliquam. Proin accumsan lectus id massa vehicula eleifend. Quisque ac dui a eros dapibus lacinia ac ut odio. Maecenas vel tempus nibh. Quisque neque neque, aliquet nec dignissim non, aliquet sed arcu. Integer et fermentum ipsum. Morbi tempus, libero sed luctus varius, lorem metus feugiat tortor, sit amet dictum lacus odio quis enim. Curabitur sed nisl eget urna laoreet porttitor eu at massa. Nam volutpat hendrerit nulla quis elementum. In in adipiscing urna. Nam sagittis metus sit amet eros ornare rutrum. Mauris dapibus dignissim nisl non blandit. Morbi pharetra lobortis placerat. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce dapibus lacus et augue tincidunt placerat pellentesque elit accumsan. Aenean sed dui mauris. Aliquam vehicula mollis eros non sodales."; echo intro($content);
Create an account or sign in to comment