2012-10-31 3 views
-1

데이터베이스에서 검색하는 문자열이 있는데 공백없이 문자열의 길이를 계산하려고하지만 실제 길이보다 큰 21 개의 문자를 표시합니다. 탭과 개행 문자는 물론 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는 내가 현재 가지고있는 순서대로 잘못된 값을 출력하고 있다는 사실을 제외하고는 훌륭하게 작동했습니다. 다음과 같이 완벽하게 작동한다는 것을 알았습니다. n은 \ r에 \ n을 & \ 중심으로 변경

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

는 않는 str_replace는 \ 연구 \ n을 떠나 그냥 \ 연구에서 \ n을을 제거하지했다.

건배.

3

mb_strlen을 사용해보십시오. http://php.net/manual/en/function.mb-strlen.php 더욱 정확합니다.

mb_strlen($str,"UTF-8") 

UTF-8이 기본 인코딩입니다 ... 모든 freespaces 그런 것을 시도 제거하려면 ..

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

미안, 작동하지 않는 동일한 결과! 내 인코딩은 "ASCII" 고맙습니다. 예, 형, 위의 시도했습니다. – irohit786

관련 문제