php函数问题
好吧,还有一个问题,然后我认为我的功能会起作用。
现在,我的第二个函数只是报告字符串旁边的字符长度,如下所示:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 str_word_count 函数来计算字符串中的单词数。
You can use the str_word_count function to count words in a string.