2012-10-31 159 views
-1

我有一个字符串,我从数据库中检索,我想计算字符串的长度没有空格,但它显示一个较大的长度值(比实际计数大21个字符)I已经删除了标签和换行符以及php和html标签,但没有结果!我已经尝试了几乎w3schools php参考的所有功能,但是我无法找到任何成功。我还观察到,如果我不从数据库中检索输入值这样的:Strlen没有给出正确的输出

$string = "my string";

我得到正确的长度,请帮助我。下面是代码:

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; 
} 

这是从数据库中字符串,但你可以看到上面我已删除了使用strip_tags()所有php和html标签:

<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> 

,当我打印出来,它显示为:

<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> 
+0

如果您正在浏览器中查看,请确保没有任何html标签以及其中的内容。只是因为你看不到多余的字符意味着很少。 –

+0

有额外的标签,我已经删除了使用strip_tags() – irohit786

+0

我不明白...我用你输入的输入运行你的代码,它对我来说工作正常。它必须与你从db得到的东西有关......你需要添加更多的代码,尝试从db直接复制文本或类似的东西。 – hummingBird

回答

4

@Svetilo,不粗鲁只是想后我发现,你的str_replace工作很好,除了我仍然按照你现在的顺序输出不正确的值,我发现以下工作完美无缺。

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

围绕\ r \ n & \将N-所作的str_replace函数不去掉从\ r \ n离开它只是一个\ r成\ n。

干杯。

3

尝试使用mb_strlen。 http://php.net/manual/en/function.mb-strlen.php 它更精确。

mb_strlen($str,"UTF-8") 

凡UTF-8是你的默认编码... 要删除所有空格自由尝试类似的东西..

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

之上,对不起,不起作用,结果相同! 和我的编码是“ASCII” thanx,是兄弟,我已经试过上面。 – irohit786