Count number of chars excluding newlines

3k views Asked by At

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?

3

There are 3 answers

0
dadarya On BEST ANSWER

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

echo strlen(str_replace(array("\n", "\r\n", "\r"), '', $string));
2
Shibby On

you can subtract one, like this:

$x = stlen(str);
$x--;
0
joar On
<?php
$string = "Testing \n testing";


$x = strlen(str_replace(array("\n","\r","\r\n"), "", $string)); //replace '\n \r' with nothing before counting the rest