2

I am trying to find the count of following text in php using strlen() function,

<?php
    $a=" VII,C :06/11 ENG:TRANSLATION HUMAN BODY STRUCTURE
    TAM:FINISH CW &PULAVAR BOOK
    MAT:LEARN LN-1
    SOC:GEO:-1 CHOOSE,FILL UPS&MATCH
    SCI:MATTER&ITS NATURE.
    ";
?>

It returns 155 character length.

you can run the sample here http://codepad.org/qIJ0fBEj

and I removed the five enter spaces and run the below code,

<?php
$a=" VII,C :06/11 ENG:TRANSLATION HUMAN BODY STRUCTURETAM:FINISH CW &PULAVAR BOOKMAT:LEARN LN-1SOC:GEO:-1 CHOOSE,FILL UPS&MATCHSCI:MATTER&ITS NATURE.";
echo strlen($a);
?>

check the sample here http://codepad.org/JDFy6Y1t

It returns 145. How the 5 enter keys takes 10 count??

John Conde
  • 217,595
  • 99
  • 455
  • 496
Akilan
  • 1,707
  • 1
  • 16
  • 30

2 Answers2

4

I bet this input came from a Windows computer. The new line is actually a carriage return (\r) and a new line character (\n). Thus two characters for each new line.

John Conde
  • 217,595
  • 99
  • 455
  • 496
1

+1 for John, carriage return is the issue. You can use str_replace to remove all the carriage returns from the string.

$a = str_replace("\r\n", "\n", $a); // in windows
Fawzan
  • 4,738
  • 8
  • 41
  • 85