There are 2 codes, PHP and JS. The work of the codes is exactly the same.
The duty of the codes is to count words. However, there is 1 difference between PHP and JS results.
The reason for this difference is due to the "line". How can I solve this problem.
I want both codes to give the same values.
PHP CODE:
PHP Result: 19
<?php
    function character_count($str){
        $str = preg_replace("/<\/?[a-z][^>]*>/i", "", $str);
        $str = preg_replace("/ +/", " ", $str);
        $str = preg_replace("/\n+/", "", $str);
        $str = mb_strlen($str,'UTF-8');
        return $str;
    }
    
    $string = '<p>Hello World</p>
<p>Welcome</p>';
    
    echo character_count($string);
PHP CODE TEST: https://sandbox.onlinephpfunctions.com/code/64dac96d1406e2e7352858f8563f82378929eeb3
JS CODE:
JS Result: 18
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<h1></h1>
<script>
    function character_count(str){
        return str ? str.replace(/<\/?[a-z][^>]*>/gi, "").replace(/ +/g, " ").replace(/\n+/, "").length : 0;
    }
    
    var string = '<p>Hello World</p>\n' +
        '<p>Welcome</p>';
    
    $("h1").text(character_count(string));    
</script> 
    
` in `js` no.
– Simone Rossaini Sep 29 '20 at 06:24