Jump to content

Recommended Posts

Hello,

I know this has been answered A LOT of times but I can't remember how to do it.

Can someone tell me how I would cut off a string:

e.g.

$string = "Hello everyone at PHP Freaks!"

and cut it off at a certain amount of characters and add "..." at the end of the string when it is echo'ed.

e.g

Hello everyone at PH... click here read more

 

Any help would be appreciated.

 

regards

Link to comment
https://forums.phpfreaks.com/topic/48028-solved-cut-off-and-add-a/
Share on other sites

how right you are... lol... i post this one here all the time...

we should make a database here of these functions lol

 

<?
function filter_charlimit($string, $length="50"){
if(strlen($string)<$length) return $string;
else return trim(substr($string, 0, $length)).'...';
}
?>

I have found a problem with this.

When I have a html link and it just happens to end the string to put the ... in. It comes out:

href="http://www.bu...%3Ca%20href=" morenews.php?morenews="1"">More Info</a>

How can I stop it from adding the .. more info link when it is in the middle of a html tag?

It isnt always used for a link. Just sometimes there is a link in the place it gets cut off.

The code I am using is:

if(!function_exists(filter_charlimit)) 
{
function filter_charlimit($string,$length,$addition)
	{
		if(strlen($string)<$length) return $string;
		else return trim(substr($string, 0, $length)).$addition;
	}
}

and in the page:

<?php echo filter_charlimit($news_content,100,'...<a href="morenews.php?morenews='.$news_id.'">More Info</a>'); ?>

is this what you're looking for?

<?php
        function truncate($string, $length, $url){
                        if(strlen($string) > $length){
                                return $string = substr($string, 0, $length) . "...<a href=\"{$url}\">Click here for more</a>";
                        }else{  
                                return $string .= "...<a href=\"{$url}\">Click here for more</a>";
                        }
        }
?>

http://ca.php.net/manual/en/function.strip-tags.php

 

That actually allows you to specify tags to leave 'as is'.  Why would you have br tags in your database anyway?  Normally, the database entries contain 'newlines' and you convert those when necessary to html breaks by using the nl2br() function.

 

If you have data with html links in AND you want them to remain then you're going to have to accept a character limit that's specified in a different way.  Perhaps, explode the database on </a> or use strpos() to find where those exist.

 

Bottom line: fully define the problem before you begin looking for solutions - on your own or with help here.

br tags are in the db because I use nl2br() when I use a text area in a certain section of the site.

Thanks for the info. I will try it.

 

Also I've become to notice people have become hostile around here. There's no need for it. I'm not forcing you to help me.

People also forget that members like me do actually give back to the community in the same way.

I think I will look elsewhere for my help next time, and just constantly give to the community rather than taking.

br tags are in the db because I use nl2br() when I use a text area in a certain section of the site.

Thanks for the info. I will try it.

 

Also I've become to notice people have become hostile around here. There's no need for it. I'm not forcing you to help me.

People also forget that members like me do actually give back to the community in the same way.

I think I will look elsewhere for my help next time, and just constantly give to the community rather than taking.

 

don't be so bitter papaface. i'm working on a function for you right now. i would have had it done by now, but i got side-tracked by the ganja. i'll post a working func soon.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.