Using "Count" for variables

Discussion in 'PHP' started by jayg5000, Dec 20, 2006.

  1. #1
    For some reason I can't get the "count" function to work in php. All I want to do is count the number of characters that have been stored as a variable and then print the result. Any one have a simple solutions to a simple question?

    Here is my code:
    if(isset($dat["Hype"]))
    		{
                            $descri = $dat["Hype"];
                            print $descri;
    			print count($descri);
    		}
    Code (markup):

     
    jayg5000, Dec 20, 2006 IP
    TheHoff likes this.
  2. TheHoff

    TheHoff Peon

    Messages:
    1,530
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    0
    #2
    strlen not count! count is analogous to sizeof (returns number of entries in an array)
     
    TheHoff, Dec 20, 2006 IP
  3. shae

    shae Peon

    Messages:
    107
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    count() is a mysql function.
     
    shae, Dec 20, 2006 IP
    nico_swd likes this.
  4. jayg5000

    jayg5000 Active Member

    Messages:
    223
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #4
    Perfect! That works, obviously. Here comes the second question. I didn't think this through correctly and I want to use that fuction so that I can limit the characters being displayed. I have descriptions on a few products that I want to limit to a specific unified size. I can use the strlen to count the variable now how do I limit what that variable prints due to the results on the count? Thanks, this is helping a ton!
     
    jayg5000, Dec 20, 2006 IP
  5. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #5
    You will want to use substr.

    Not sure how long you want to limit your string to.

    
    
    $newString = substr("$oldString", 0, 200);
    
    
    PHP:
    Here's the substr manual.
    http://php.net/substr
     
    jestep, Dec 20, 2006 IP
  6. jayg5000

    jayg5000 Active Member

    Messages:
    223
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #6
    Thanks! That did it!
     
    jayg5000, Dec 20, 2006 IP
  7. TheHoff

    TheHoff Peon

    Messages:
    1,530
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Here is a function I use that is kinder to the description. It will only cut off at the end of a word or a sentence and it will add ellipses to the end. It only takes two variables, the string and the cut off point.

    $newstring = shorten ($oldstring, length);

    
    function shorten($string,$length)
    {
    	if (strlen($string)>$length)
    	{	
    		$cutnum = $length-2;
    		$sample = substr($string,$cutnum,1);
    
    		while ( ($sample!=' ' AND $sample!='.') AND ($cutnum < $length+10) )
    		{
    			$cutnum = $cutnum + 1;
    			$sample = substr($string,$cutnum,1);
    		}
    		$string = substr($string,0,($cutnum))."...";
    	}
    	return $string;
    }
    
    Code (markup):
     
    TheHoff, Dec 20, 2006 IP