Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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