phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


String: Truncating String

Tag: basic, string   Category: PHP Basic
post: 01 Feb 2008 read: 196


PHP String Tips: As you can see hot post at right side this web, there are several title that have a lot of character. So, I decide to cut them and make them below 45 character. If they have more than 45 characters, after cut, add "..." later. How we create it?

First, create function like this:

function truncateString($str, $maxChars='45',$holder="..."){
	if(strlen($str) > $maxChars){
		return trim(substr($str,0,$maxChars)).$holder;
	}else{
		return $str;
	}
}

Then, we can use like this:

<a href="test.html" title="test"><?=truncateString($rp['title'])?></a>



| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


615
posting