Jump to content

Archived

This topic is now archived and is closed to further replies.

Kingy

[SOLVED] String Length

Recommended Posts

Ok i have 3 varibles that i want to join together and then i want it to count them.

 

<?php
       $string = "$ex[3] "." $ex[4] "." $ex[5]";
       $string = trim($string);
       $length = strlen($string);
      
       echo $string;
       echo $length;
?>

 

if i use the example

 

`string hello my name is kingy

it returns

 

`string hello my name is kingy

32

 

But if you count, there should only be 30 characters :s

Share this post


Link to post
Share on other sites

You could have extra spaces btween the words. trim() only removes from the ends.

Share this post


Link to post
Share on other sites

oh yeah i think i see now the output is actually

 

String: `string  hello  my name is kingy

 

2 spaces either side of hello... how do i fix this :s

 

`string = $ex[3]

hello = $ex[4]

my name is kingy = $ex[5]

 

so i have stuffed up my spaces in between the $ex somehow

Share this post


Link to post
Share on other sites

Change the spaces here

 

$string = "$ex[3] "."$ex[4] "."$ex[5]";

Share this post


Link to post
Share on other sites

Try this:

 

<?php
       $string = "$ex[3] "." $ex[4] "." $ex[5]";
       $string = trim($string);
       $exp = explode(" ",$string);
       $len = 0;
       foreach($exp as $val){
              $len += strlen($val);
       }
       echo $string;
       echo $len;
?>

Share this post


Link to post
Share on other sites

×
×
  • 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.