0

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;
    }
    }
    ?>
4
  • 6
    I don't see any correlation between the question and the code snippet Commented Jul 10, 2011 at 17:11
  • Do you use var_dump somewhere? That is for debugging and no, you can't change that format. But you can make your own output. Commented Jul 10, 2011 at 17:15
  • looks like he's just var_dumping the returned value and wants to reformat what var_dump gives Commented Jul 10, 2011 at 17:16
  • @ yi_H To clarify, my function is being used to replace words and then the completed string (with replaced words) should be returned with the word count. Does that help any? Commented Jul 10, 2011 at 17:18

1 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 I wasn't aware of the str_word_count() function. Very handy!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.