Jump to content

[SOLVED] String Length


Kingy

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

Link to comment
https://forums.phpfreaks.com/topic/86388-solved-string-length/
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

Link to comment
https://forums.phpfreaks.com/topic/86388-solved-string-length/#findComment-441459
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;
?>

Link to comment
https://forums.phpfreaks.com/topic/86388-solved-string-length/#findComment-441462
Share on other sites

Archived

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

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