php函数问题

发布于 2024-11-19 11:34:34 字数 1505 浏览 0 评论 0原文

好吧,还有一个问题,然后我认为我的功能会起作用。

现在,我的第二个函数只是报告字符串旁边的字符长度,如下所示:

    string(20) "testing testing nice"

是否有办法更改该返回的格式?我需要更改什么才能获得字数?

是否可以使格式看起来像这样:

   string word count: 3 character count: 20 "testing testing nice"

谢谢

file1.php

    <?php
    require('myfunctions.php');
    if($_POST) {
    $result = phptest($_POST['input']);
    if ($result === false) {
    echo 'No mean words were found.';
    } else {
    var_dump($result);
    }
    }
    ?>

    <?php
    echo "<br/> Sum function: ".sum(1,2,3,4)."<br/>";
    echo "Average function: ".average(1,2,3,4)."<br/>";
    ?>

    <form action="" method="post">
    <input name="input" type="text" size="20" maxlength="20" />
    <input name="submit" type="submit" value="submit" />
    </form>

myfunctions.php

    <?php
    function sum() {
    return array_sum(func_get_args());
    }

    function average() {
    $args = func_num_args();

    if ($args == 0) {
    return 0;
    }

    return array_sum(func_get_args()) / $args;
    }  
    ?>

    <?php
    function phptest($input) {
    $search = array('ugly', 'rude');
    $replace = array('nice', 'sweet');
    $output = str_ireplace($search, $replace, $input, $replace_count);
    if ($replace_count === 0) {
    return false;
    } else {
    return $output;
    }
    }
    ?>

OK, one more problem and then I think my function will work.

Right now, my second function is just reporting the character length back next to the string like so:

    string(20) "testing testing nice"

is there anyway to change the format of that return? And what do I need to change to get the WORD count too?

Is it even possible to make the format look like this:

   string word count: 3 character count: 20 "testing testing nice"

thanks

file1.php

    <?php
    require('myfunctions.php');
    if($_POST) {
    $result = phptest($_POST['input']);
    if ($result === false) {
    echo 'No mean words were found.';
    } else {
    var_dump($result);
    }
    }
    ?>

    <?php
    echo "<br/> Sum function: ".sum(1,2,3,4)."<br/>";
    echo "Average function: ".average(1,2,3,4)."<br/>";
    ?>

    <form action="" method="post">
    <input name="input" type="text" size="20" maxlength="20" />
    <input name="submit" type="submit" value="submit" />
    </form>

myfunctions.php

    <?php
    function sum() {
    return array_sum(func_get_args());
    }

    function average() {
    $args = func_num_args();

    if ($args == 0) {
    return 0;
    }

    return array_sum(func_get_args()) / $args;
    }  
    ?>

    <?php
    function phptest($input) {
    $search = array('ugly', 'rude');
    $replace = array('nice', 'sweet');
    $output = str_ireplace($search, $replace, $input, $replace_count);
    if ($replace_count === 0) {
    return false;
    } else {
    return $output;
    }
    }
    ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妄断弥空 2024-11-26 11:34:34

您可以使用 str_word_count 函数来计算字符串中的单词数。

function str_info($string) {
  $words = str_word_count($string);
  $chars = strlen($string); //Consideer using mb_strlen if you're using Unicode
  return 'string word count: '. $words .' character count: '. $chars .' '. $string;
}

You can use the str_word_count function to count words in a string.

function str_info($string) {
  $words = str_word_count($string);
  $chars = strlen($string); //Consideer using mb_strlen if you're using Unicode
  return 'string word count: '. $words .' character count: '. $chars .' '. $string;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文