Count words like Microsoft Word

I need to count the words in a string using PHP or Javascript (preferably PHP). The problem is that the count should be the same as in Microsoft Word, because that's where people collect their original texts, so this is their frame of reference. PHP has a word counting function ( http://php.net/manual/en/function.str-word-count.php ), but it's not 100% as far as I know.

Any pointers?

+3
source share
5 answers

The real problem is that you are trying to develop a solution without understanding the exact requirements. This is not a coding problem, but a specification problem.

, Word - , , . , " Word "? , - , , . , , .

, , , , . , , , , (, True Way).

Word - -, - , , , . , ...: -/

+9

MS , - , , , , .

, - http://dotnetperls.com/word-count

, #, php.

, : MS Word , " ", , , , . , , EVIL messing with hypens . "el-dash" "em-dash" "".

+2
function countWords( $text )
{
    $text = preg_replace('![^ \pL\pN\s]+!u', '', strtolower($text));
    $text = trim( preg_replace('![ \s]+!u', ' ', $text) );

    $count = count( explode(' ', $text) );

    return $count;
}
0
source

you can use this code to count words

<title>Untitled Document</title>
<script type="text/javascript" src="mootools.svn.js"></script>
<script type="text/javascript">
    window.addEvent('domready', function()
    {   
        $('myInput').addEvent('keyup', function() 
        {
            max_chars = 0;
            current_value   = $('myInput').value;
            current_length  = current_value.length;
            remaining_chars = max_chars+current_length;
            $('counter_number').innerHTML = remaining_chars;
            if(remaining_chars<=5)
            {
                $('counter_number').setStyle('color', '#990000');
            } else {
                $('counter_number').setStyle('color', '#666666');       
            }   
        }); 
    }); 
</script>

<style type="text/css"> 
    body{
        font-family:"Lucida Grande", "Lucida Sans Unicode", Verdana, Arial, Helvetica, sans-serif; 
        font-size:12px;
        color:#000000; 
    }
    a:link, a:visited{color:#0066CC;}
    label{display:block;}
    .counter{
        font-family:Georgia, "Times New Roman", Times, serif;
        font-size:16px; 
        font-weight:bold;
        color:#666666
    } 
</style> 
</head>
<body> 
    <label for="myInput">Write something here:</label> 
    <input type="text" id="myInput" maxlength="20" />  
    <span id="counter_number" class="counter">20</span> 
    Remaining chars

and download the mootools library ...

0
source

The following JS code gives a word count of 67. OpenOffice gives the same number.

str = "I need to count words in a string using PHP or Javascript (preferably PHP). The problem is that the counting needs to be the same as it works in Microsoft Word, because that is where the people assemble their original texts in so that is their reference frame. PHP has a word counting function (http://php.net/manual/en/function.str-word-count.php) but that is not 100% the same as far as I know.";

wordCount = str.split(/\s+/g).length;
0
source

Source: https://habr.com/ru/post/1730178/


All Articles