php计算文本字符串中单词数量

php计算文本字符串中单词数量

php实例qingyu2021-05-31 15:13:391689A+A-

  php计算文本字符串中单词数量

  在本实例中,用户在文本域中输入英语整句后,提交表单可以返回句子中单词的个数。用户可以非常方便的得知所输入字符串的长度。

  1.实现过程:

  在页面中添加表单和显示区域结构。本实例核心函数为str_word_count(),此函数用来返回字符串中单词的个数。

  2.实例代码:

<?php
if(!empty($_POST['word'])){
    $str = $_POST['word'];
    $num = str_word_count($str);
    echo "<script>alert('单词数量为:{$num}个')</script>";
}
?>

<!doctype html>
<html lang="en">
<head>

</head>
<body>
    <div id="content">
        <form action="" method="post">
            <h3>计算字符串单词数量</h3>
            <div>
                <input type="text" value="" size="100" name="word">
            </div>
            <div>
                <input type="submit" value="提交" name="btn">
            </div>
        </form>
    </div>
</body>
</html>


  3.运行结果:

php计算文本字符串中单词数量

点击这里复制本文地址 欢迎来到大黄鸡源码分享网
qrcode

大黄鸡源码编程网 © All Rights Reserved.  
网站备案号:闽ICP备18012015号-4
Powered by Z-BlogPHP
联系我们| 关于我们| 广告联系| 网站管理