0

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("/&nbsp;+/", " ", $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(/&nbsp;+/g, " ").replace(/\n+/, "").length : 0;
    }
    
    var string = '<p>Hello World</p>\n' +
        '<p>Welcome</p>';
    
    $("h1").text(character_count(string));    
</script>
  • 2
    the string is not same, in `php` you have a space between two `

    ` in `js` no.

    – Simone Rossaini Sep 29 '20 at 06:24
  • You mean the difference is because the php counts new lines too? If yes then change the `replace(/\n+/, "")` in the js function to the `replace(/\n+/, " ")`. Then the problem will be solved – Ashkan Sep 29 '20 at 06:25
  • i think yes, if you try to delete that space the count is same. – Simone Rossaini Sep 29 '20 at 06:26
  • the new line is counted too – dragos.nicolae Sep 29 '20 at 06:29
  • @Ashkan The problem was resolved when I made the change you specified. I'm doing a few tries. Thanks – Morgan Codes Sep 29 '20 at 06:34
  • @Ashkan ```.replace(/\n+/, " ")``` The problem is solved again when I delete it from the JS section. Will there be a different problem later ? What is the meaning of this ? – Morgan Codes Sep 29 '20 at 06:38
  • As @SimoneRossaini said, the string in php code is different from the string in js. Use this `$str = preg_replace("/\r\n+/", "", $str);` instead of `$str = preg_replace("/\r\n+/", "", $str);` in php function and use the previous `replace(/\n+/, "")` for js function. then the results would be same: 18 – Ashkan Sep 29 '20 at 06:44
  • @Ashkan after this change, the results change a lot. – Morgan Codes Sep 29 '20 at 07:00
  • Line breaks in PHP are matched with `\R`, so use `preg_replace("/\R+/", "", $str);`. In JS, to match any line break chars you need `/[\r\n\x0B\x0C\u0085\u2028\u2029]+/g` – Wiktor Stribiżew Sep 29 '20 at 08:14
  • `function character_count($str){ $str = preg_replace("/<\/?[a-z][^>]*>/i", "", $str); $str = preg_replace("/ +/", " ", $str); $str = preg_replace("/\R+/", "", $str); $str = mb_strlen($str,'UTF-8'); return $str; }` – Morgan Codes Sep 29 '20 at 19:50
  • `function _character_count(str){ return str ? str.replace(/<\/?[a-z][^>]*>/gi, "").replace(/ +/g, " ").replace(/[\r\n\x0B\x0C\u0085\u2028\u2029]+/g, "").length : 0; }` – Morgan Codes Sep 29 '20 at 19:50
  • @WiktorStribiżew These last changes I made are working properly. Thank you. – Morgan Codes Sep 29 '20 at 19:50

0 Answers0