CodeCharge Studio
search Register Login  

Visual PHP Web Development

Visually Create Internationalized Web Applications, Web Reports, Calendars, and more.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> PHP

 Restrict the number of characters in a label [ Sorted ]

Print topic Send  topic

Author Message
Wreckmaster

Posts: 48
Posted: 11/04/2012, 6:18 AM

Is there a way or code, to restrict the number of characters in a label to, 60 then end with ....
I think I read a post on this before but can't find it now.

Thank you.
View profile  Send private message
saseow

Posts: 744
Posted: 11/04/2012, 7:22 AM

Try this In before show:

global $str;
$str=$Component->GetValue();
if (strlen($str)>60){
$Component->SetValue(substr($Component->GetValue(),1, 10)."....");
}
View profile  Send private message
bannedone


Posts: 273
Posted: 11/04/2012, 8:45 AM

Hi

I did it this way since I did not want words to be separated. This function limits the string to the length required but breaks at the nearest space.

Also my content for the label could have been HTML so this also dealt with stripping all that out.

First I created 2 functions.

  
  
/* -------------------------------------------------------------------------------------------  
   Function:  
      limitText  
     
   Description:  
      Create a String of fixed length  at word boundaries & strip slashes  
    
   Params  
      $String - Input string  
	  Length - Length of strin to return..  No value just strips slashes  
  
   Returns  
      Clean String (Slashes stripped) of desired length  
  
   ------------------------------------------------------------------------------------------- */  
function limitText($String,$length=0){  
  
$TString=$String;  
  
//$TString=urldecode($TString);  
  
if($length > 0){  
$i=1;  
$FLV=ExtractString($TString,'<div id="player',"</div>");  
if($FLV){  
//echo "It ".$FLV;  
$TString=str_replace($FLV,'">',$TString);  
$TString=str_replace("var s".$i." = new","",$TString);  
}  
  
}  
  
if(stristr($TString,"<a")){$a=1;}else{  
$TString=str_replace("\n","<br/>",$TString);  
$TString=str_replace("\r","",$TString);  
  
$TString=stripslashes($TString);  
$TString=stripslashes($TString);  
$TString=stripslashes($TString);  
}  
  
if ($length==0) return $TString;  
  
$TString=strip_tags($TString);  
$TString=substr($TString,0,$length);  
$TString=strrev($TString);  
$TString=substr($TString,strpos($TString," ")+1);  
$TString=strrev($TString)."....";  
  
return $TString;  
}  
  
  
/*  
Function ExtractString($str,$start,$end)  
Retruns The value of a string between two strings of tags  
  
$str = Sthring to search in  
$start = First Tag to start search between  
$end   = End Tag to search between  
  
Returns $string between the start and end tags  
		null if not found  
  
*/  
  
function ExtractString($str, $start, $end)  
{  
$str_low = strtolower($str);  
$pos_start = @strpos($str_low, $start);  
$pos_end = @strpos($str_low, $end, ($pos_start + strlen($start)));  
if ( ($pos_start !== false) && ($pos_end !== false) )  
{  
$pos1 = $pos_start + strlen($start);  
$pos2 = $pos_end - $pos1;  
return substr($str, $pos1, $pos2);  
}  
}  
  

Then in the label before show add this Custom Code

$Container->your_label_control_name->SetValue(limitText($Container->your_label_control_name->GetValue(),60));

Have Fun
8-)
_________________
John Real
CodeCharge Studio Support, Training, Consulting, Development, Web based solutions
http://realsites.biz
http://ccselite.com
Other Banned IDs on this Forum. jjrjr1, jjrjr2
View profile  Send private message
Wreckmaster

Posts: 48
Posted: 11/04/2012, 9:41 AM

Saseow, the code as you have given it here .

when it finds text which is longer the 60 characters it strips out the first character and reduces the rest to 9 in total.
But if I use the following I get the result I was looking for.

global $str;
$str=$Component->GetValue();
if (strlen($str)>60){
$Component->SetValue(substr($Component->GetValue(),0, 60)."....");
}

Bannedone
I like what your function does so will give that a try now.
do I put the function in the class file or should I put it on the events page.

Thanks to both of you for your help.
View profile  Send private message
bannedone


Posts: 273
Posted: 11/04/2012, 9:49 AM

You can either put it in the bottom white space in Common.php or do What I usually do.

I have created a plain php file called comfunk.php where I put all the common functions I create. This is sort af a library of special functions I have created over the years and are very useul.

Then in the final white space in Common.php I simply include this file of functions.

This has been very useful to me over the years as my comfunk.php is full of very useful functions that I have created over the years

FYI.. The reason for the bottom of Common.php or include file at the bottom of Common.php is by doing this ALL of the CCS functions are available to you when writing special functions for use in your application.

btw here is an implementation of the code I posted

http://tribalentertainment.com

You can see it working in the right and left sidebars.

The reason for my functions removing HTML is so it displays correctly for the sidebars but when you click more the regular content shows as that can contain HTML as inserted by the site admin. (I Used FCKeditor for site content management)

8-)


_________________
John Real
CodeCharge Studio Support, Training, Consulting, Development, Web based solutions
http://realsites.biz
http://ccselite.com
Other Banned IDs on this Forum. jjrjr1, jjrjr2
View profile  Send private message
Wreckmaster

Posts: 48
Posted: 11/04/2012, 11:13 AM

Thanks bannedone
Yes works perfectly, unfortunately some of my texts are less the 60 character's and the function adds the .... to thes too .
Good tip on the comfunk.php , I have lots of code snippets here there and every where , time to tidy up.

Thanks again
View profile  Send private message
bannedone


Posts: 273
Posted: 11/04/2012, 11:16 AM

Glad to help.

In my implementation I wanted the .... in all cases at the end.

Easy modification to not add the ... if less than the required length.

Have fun
8-)

_________________
John Real
CodeCharge Studio Support, Training, Consulting, Development, Web based solutions
http://realsites.biz
http://ccselite.com
Other Banned IDs on this Forum. jjrjr1, jjrjr2
View profile  Send private message
saseow

Posts: 744
Posted: 11/06/2012, 3:29 AM

bannedone, nice idea!

Thanks.
View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

PHP Reports

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.