Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

php script for truncating text at the end the sentense or a word

Featured Replies

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

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

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 by andyl

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 :mellow: i may use it for a project

This is actually quite a nice function :mellow: 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 :good:

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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.