2012-10-31 157 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