Trouble truncating information drawn from a database using strrpos

Hi there.

I have created a widget on my webpage that shows customer feedback, which is held on a database.

The div that displays this information is a fixed size so I need to truncate the longer messages and cut it back to the last full word before the character cut off point.

Unfortunately I am stuck.

This is what I have come up with so far:

					<p class="quotes"><? echo strrpos($rows['message'],0,5);?></p>

I have connected to the database correctly, and without the strrpos function, the text displays just fine. But the longer messages, ruin the webpage.

I am a total newbie when it comes to php, so be gentle. Php has only been in my life for a couple of weeks.

Any help would be greatly appreciated. If you could explain where I have gone wrong, it would really help me out.

Many thanks.

I found this function somewhere on i think, not sure, but it works
[php]function truncate($string, $length = ‘’, $replacement = ‘…’, $start = 100) {
if(strlen ($string) <= $start)
return $string;

if($length) {
	return substr_replace($string, $replacement, $start, $length);
} else {
	return substr_replace($string, $replacement, $start);


[/php]$start = 100 is how many characters you want to keep on the page.

Thanks for the reply Richei.

Just a couple of questions though.

Would I place this piece of code under the while loop on my page and then call the function instead of using the echo statement ?

Also, on line 12, should I write $rows instead of $r ?

Thanks mate.

Or someting like:


echo ($rows[‘message’];

I use it with the echo. echo truncate($row[message])

Thanks for all your help Richei.

I will give it a try when I get home from work and let you know how it goes.

Thanks again.

Hi Richei.

Your code worked a treat. However, I settled on some code I found that enabled me to keep it as one line.

[php]<? echo substr( $rows['message'], 0, strrpos(substr($rows['message'],0, 30), ' '));?>[/php]

This is working for me, but I wonder if I could pick your brains some more.

First, I want to add 3 full stops after ['message] has been truncated. For example - the quick brown fox…
Just to give the effect of the text continuing if you click it.

And finally, I want to be able to make the message a link so when clicked, it takes the user to a page displaying [‘message’] in full.

I think this needs to be done using the id of the row to build a dynamic link. Any advice you could offer on this would be greatly appreciated and enable me to put this part of my site to bed.

Thanks in advance.

I don’t know why you tried to put all my code into 1 line, what i gave you was a function, put it on top of the page and its good to go, and it does the periods at the end. But to answer your question about the link,
[php]<?php echo "".substr( $rows['message'], 0, strrpos(substr($rows['message'],0, 30), ' '))."";?>[/php]

and short tags (<? ?>) are generally not turned on by default on regular servers, don’t get into the habit of using them.

Hi Richei.

Your function was very helpfull. But I chose the one line example because it it came with a step by step tutorial and explained every step. Because of this, I just found it easier to understand.

Thanks for the advice regarding the short tags. I will make sure I don’t use them.

Sponsor our Newsletter | Privacy Policy | Terms of Service