1

I'm using strlen() to count the number of chars in a text, but it even counts newline \n. Can i replace newlines is some way to make strlen() don't count them?

Antoder
  • 65
  • 2
  • 9
  • 1
    Considere adding code to your question ;) For your problem, you could copy your string to a temporary one, strip the newlines from it then count the number of char in this temporary string – Veve Nov 22 '14 at 17:41
  • 1
    `strlen(str_replace("\n", "", $str));` – DarkBee Nov 22 '14 at 17:45

3 Answers3

5

use this first replace all new line character by null and than count length.

echo strlen(str_replace(array("\n", "\r\n", "\r"), '', $string));
dadarya
  • 313
  • 1
  • 6
1
<?php
$string = "Testing \n testing";


$x = strlen(str_replace(array("\n","\r","\r\n"), "", $string)); //replace '\n \r' with nothing before counting the rest        
dadarya
  • 313
  • 1
  • 6
joar
  • 312
  • 2
  • 11
-4

you can subtract one, like this:

$x = stlen(str);
$x--;
Shibby
  • 88
  • 2