문제

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.

Any pointers?

도움이 되었습니까?

해결책

The real problem here is that you're trying to develop a solution without really understanding the exact requirements. This isn't a coding problem so much as a problem with the specs.

The crux of the issue is that your word-counting algorithm is different to Word's word-counting algorithm - potentially for good reason, since there are various edge-cases to consider with no obvious answers. Thus your question should really be "What algorithm does Word use to calculate word count?" And if you think about this for a bit, you already know the answer - it's closed-source, proprietary software so no-one can know for sure. And even if you do work it out, this isn't a public interface so it can easily be changed in the next version.

Basically, I think it's fundamentally a bad idea to design your software so that it functions identically to something that you cannot fully understand. Personally, I would concentrate on just developing a sane word-count of your own, documenting the algorithm behind it and justifying why it's a reasonable method of counting words (pointing out that there is no One True Way).

If you must conform to Word's attempt for some short-sighted business reason, then the number one task is to work out what methodology they use to the point where you can write down an algorithm on paper. But this won't be easy, will be very hard to verify completely and is liable to change without notice... :-/

다른 팁

SharePoint에는 50,000 개의 고유 한 권한의 하드 제한 목록 당 정의되고 조직이 중요한 크기 또는 (더 많은 경우) 중요한 회전율이면 해당 한도를 누릴 수 있습니다. 항목 수준 보안을 만드는 자동화 된 솔루션을 만드는 것은 해당 한도를 더 빨리 때리게 할 수 있습니다.

보다 복잡한 보안이 더 느려지는 전체 사이트 성능이 느려지므로 검색을 둔화시키고 이러한 시나리오에서 보안을 관리하려고 시도합니다.

waqas가 지적한 것으로 나타 났지만 실제로 요구 사항이면 SharePoint가 자신이 시도하고자하는 것을 수행하는 가장 좋은 도구가 아닙니다. SharePoint-Land에 멈추는 경우 사용자 정의 CAML 쿼리를 사용하여 백 엔드 데이터를 호출하는 일종의 사용자 정의 프런트 엔드를 만들어야합니다. 오, 목록 설정에서 특정 목록의 색인 생성을 끄면 간단한 크롤링에서 찾을 수 없도록하십시오.

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;
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;
}

you can use this code for word count

<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...

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top