PHP – Counting the number lines in a string – 2 Solution

Counting the number lines in a string with explode function:

<?php
$lines = explode(“\n”, $string);
$nr_lines = count($lines)-1;
?>

Counting the number lines in a string with substr_count function:

<?php
$nr_lines = substr_count($string,”\n”);
?>

the 2’nd solution is better.

byrev Written by:

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *