Breaking up long string from db into separate paragraphs

Hi all

2 problems really:

$row['short']=substr($row['main_txt'], 0, 142).'...';

This works great, but I want to output from the last full word not cut off in the middle.

I did try:

$row['short']=substr($row['main_txt'], 0, 142, " ").'...';

Gives the error: Warning: Wrong parameter count for substr() in…

  1. How can I break a long string from my DB into <p> on output to the webpage?

Is there a way of adding a <p> after every .(fullstop) or every 2?
And what if I want to add <p> on some strings but not other, and maybe I want to add <h1> just to confuse things?

Just trying to understand how your suppose to output long strings without adding html elements into the db to make it look right :nono:

Thanks guys :slight_smile:

Thanks ScallioXTX

Starting to get the picture :slight_smile:
Regarding summarized texts in the database, I’m only using the function, all the text in my DB is plain text. Which brings me onto the next question (if you have a minute):

Ok, I have an article(s) in my DB. In any text editor I could easily separate this article/text into <p>'s adding<strong>, <em> etc.

How can i add the html elements where I need them without adding it to the DB? I’m currently adding html code to my DB as we speak to display the output… getting very messy and if I decide to output this text else where, you can see what I mean :nono:

What is the best way to avoid this and keep my DB clean?
Would changing this to XML help in anyway?

How would you got about this? How is it done?
Thanks :cool:

Fair enough, here is the same function with comments this time :slight_smile:


/**
* Create a summary of a given input
* @param string $text The text to summarize
* @param string $length The maximum number of characters in the summary
* @param string $append The text to append to the summary
* @param bool $splitOnWholeWords Whether to split on whole words (take spaces into account) or just take $length characters
*/
function summarize($text, $length, $append = '...', $splitOnWholeWords = true)
{
	// If the length of text is shorter than the input text nothing needs to be done
	// and we can just return the input text
	if (strlen($text) <= $length) return $text;
	// initialize the $split variable
	$split = 0;
	if ($splitOnWholeWords) // If we want to split on whole words ...
	{
		// initialze variables
		$i = 0; $lplus1 = $length + 1;
		// Keep scanning the string for spaces until we end up on a position
		// in the string that is larger than $length
		while (($i = strpos($text, ' ', $i + 1)) < $lplus1)
		{
			if ($i === false) break; // There are no more spaces in the string
			$split = $i; // Split the string after $split characters
		}
	}
	else // ... otherwise, if we don't want to split on whole words
		$split = $length;

	// Take the found portion of the string and append $append
	return substr($text, 0, $split).$append;
}

And here is an example of how it works:


echo summarize('The quick brown fox jumps over the lazy dog', 37);

String to summarize:


The quick brown fox jumps over the lazy dog

Maximum number of characters to take from the string: 37

Find a space. The first space is found here:


The quick brown fox jumps over the lazy dog
   ^

We don’t have 37 characters yet, so search for the text space, which can be found here:


The quick brown fox jumps over the lazy dog
         ^

We don’t have 37 characters yet, so search for the text space, which can be found here:


The quick brown fox jumps over the lazy dog
               ^

We don’t have 37 characters yet, so search for the text space, which can be found here:


The quick brown fox jumps over the lazy dog
                   ^

We don’t have 37 characters yet, so search for the text space, which can be found here:


The quick brown fox jumps over the lazy dog
                         ^

We don’t have 37 characters yet, so search for the text space, which can be found here:


The quick brown fox jumps over the lazy dog
                              ^

We don’t have 37 characters yet, so search for the text space, which can be found here:


The quick brown fox jumps over the lazy dog
                                  ^

We don’t have 37 characters yet, so search for the text space, which can be found here:


The quick brown fox jumps over the lazy dog
                                       ^

Now we have more than 37 characters, so we stop searching and revert to the last known position of a space that resulted in less than 37 characters.


The quick brown fox jumps over the lazy dog
                                  ^

Now append the “…” to the end and the final result will be


The quick brown fox jumps over the...

(the example does not 100% accurately describe how the process works, but is a simplification to make it more clear).

Makes sense?

  1. I’ve created a function for this I use quite a lot

function summarize($text, $length, $append = '...', $splitOnWholeWords = true)
{
	if (strlen($text) <= $length) return $text;
	$split = 0;
	if ($splitOnWholeWords)
	{
		$i = 0; $lplus1 = $length + 1;
		while (($i = strpos($text, ' ', $i + 1)) < $lplus1)
		{
			if ($i === false) break;
			$split = $i;
		}
	}
	else
		$split = $length;

	return substr($text, 0, $split).$append;
}

I think the whole code is pretty self explanatory. If not, feel free to ask questions :slight_smile:

  1. You may want to take a step back and rethink this. There is no way (PHP) code can determine what should be a paragraph and what should be a header without some sort of basic understanding of the semantics of the text. And believe me, you don’t want to write a semantics parser …

thanks ScallioXTX

I think the whole code is pretty self explanatory.
Not to me :slight_smile:
Just wondering if I need all this? Surely these a small piece of code i can add to the existing code I have above?

I might be able to use this function if I knew how it worked, how can I add it to my existing code? How does it fit together? What does it all mean?

Bit of a php novice so any help much appreciated.

Leave 2. until I have 1. working I think :cool:

Yes, that’s the easiest way. Once you start a new project you can just copy that file to said new project and reuse all the functions without any problems :slight_smile:

You are, yes to all the questions above :tup:

One thing though, I wouldn’t store summarized texts in the database. Suppose that instead of 142 characters you decide you want to show 160 characters. In that case you need to modify the code and update the database to reflect that change. If you use the summarize function when displaying the text you don’t have that problem (and less columns in the database) since updating the code is enough.

As for NetBeans’ suggestion, it just shows you all the parameters you can pass to the function. You can (and should) safely remove that.

Would I be right in saying:

Create a file with all your functions and load that file on the page ready to be used by other code?

and from the summarize function i could easily add:

$row['short']=summarize($row['main_txt'], 142);
$row['header']=summarize($row['title'], 20);
$row['sub']=summarize($row['sub_txt'], 100);

… on the right track ScallioXTX?

:smiley: Works great!

Didn’t mean to sound direct, just the best way to explain ScallioXTX :cool:

I wasn’t sure where to place the function so I placed it at the top of the page, before the SELECT query anyway, is this correct? And what if i have a number of functions, do I place them above one another ready to be called?

I’ll be safe to store this function and use it when I need it for the same thing?

Ok, thanks a lot for the comments really helping.

I now have

$row['short']=summarize($row['main_txt'], 142);

Out of curiosity I’m coding in netbeans which gave me the option to add:

$row['short']=summarize($text, $length, $append, $splitOnWholeWords)($row['main_txt'], 142);

Why would I use this?

Again any help or information appreciated greatly :cool: