Search code examples
phpstringstrlen

Strlen is not giving correct output


I have a string which I retrieve from a database, I want to calculate the length of the string without spaces but it is displaying a larger value of length(21 characters greater than the actual count) I have removed tab and newline characters and also the php and html tags but no result! I have tried almost every function on the w3schools php reference but I'm unable to find any success. I also have observed that if I don't retrieve the value from the database and input it like this:

$string = "my string";

I get the correct length, please help me. Here is the code:

if($res_tutor[0]['tutor_experience']){
        $str = trim(strip_tags($res_tutor[0]['tutor_experience']));
            $str = $this->real_string($str);
            $space = substr_count($str, ' ');
            $experience  = strlen($str) -  $space;


function real_string($str)
{
    $search  = array("\t","\n","\r\n","\0","\v");
    $replace = array('','','','','');
    $str = str_replace($search,$replace,$str);
    return $str;
}

And this is the string from the database but as you can see above I have removed all php and html tags using strip_tags() :

<span class=\"experience_font\">You are encouraged to write a short description of yourself, teaching experience and teaching method. You may use the guidelines below to assist you in your writing.<br />
   <br />
   .Years of teaching experience<br />
   .Total number of students taught<br />
   .Levels &amp; subjects that you have taught<br />
   .The improvements that your students have made<br />
   .Other achievements/experience (Relief teaching, a tutor in a tuition centre, Dean&#39;s list, scholarship, public speaking etc.)<br />
   .For Music (Gigs at Esplanade, Your performances in various locations etc.)</span><br />
   &nbsp;</p>

and when I print it, it displays as:

<span class=\"experience_font\">You are encouraged to write a short description of yourself, teaching experience and teaching method. You may use the guidelines below to assist you in your writing.<br />
   <br />
   .Years of teaching experience<br />
   .Total number of students taught<br />
   .Levels &amp; subjects that you have taught<br />
   .The improvements that your students have made<br />
   .Other achievements/experience (Relief teaching, a tutor in a tuition centre, Dean&#39;s list, scholarship, public speaking etc.)<br />
   .For Music (Gigs at Esplanade, Your performances in various locations etc.)</span><br />
   &nbsp;</p>

Solution

  • @Svetilo, not to be rude just wanted to post my findings, your str_replace worked wonderfully, except for the fact that I was still outputting incorrect values with it in the order that you currently have, I found that the following worked flawlessly.

    $string = str_replace(array("\t","\r\n","\n","\0","\v"," "),'', $string);
    mb_strlen($string, "UTF-8");
    

    Changing around the \r\n & \n made the str_replace not strip out the \n from the \r\n leaving it just a \r.

    Cheers.