无法使用自定义函数格式化 PHP 中的字符串

发布于 2024-11-06 21:51:56 字数 304 浏览 1 评论 0原文

<?php
$string = 'tHis is aN unEVen string that needs to be formated properly';

// custom function created combining multiple functions
function varform($var){
   ucwords(strtolower(htlmentities(trim($var))));
   return $var;
}

$string = varform($string);
echo $string;
?>
<?php
$string = 'tHis is aN unEVen string that needs to be formated properly';

// custom function created combining multiple functions
function varform($var){
   ucwords(strtolower(htlmentities(trim($var))));
   return $var;
}

$string = varform($string);
echo $string;
?>

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

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

发布评论

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

评论(3

鼻尖触碰 2024-11-13 21:51:56

您需要将所有操作的结果分配给您要返回的变量

<?php
$string = 'tHis is aN unEVen string that needs to be formated properly';

// custom function created combining multiple functions
function varform($var){
   // assign change to the variable
   $var = ucwords(strtolower(htmlentities(trim($var))));
   return $var;
}

$string = varform($string);
echo $string;
?>

You need to assign the result of all your manipulations to the variable you are about to return

<?php
$string = 'tHis is aN unEVen string that needs to be formated properly';

// custom function created combining multiple functions
function varform($var){
   // assign change to the variable
   $var = ucwords(strtolower(htmlentities(trim($var))));
   return $var;
}

$string = varform($string);
echo $string;
?>
_畞蕅 2024-11-13 21:51:56

您的代码有两个问题

function varform($var){
   ucwords(strtolower(htlmentities(trim($var))));
------------------------^ //It's htmlentities() not htlmentities()
   return $var; //you're just returning the value that is passed to the method
}

您需要从 PHP 方法获取返回值并从函数返回该值

function varform($var){
   $var = ucwords(strtolower(htmlentities(trim($var))));
   return $var;
}

There are two problems with your code

function varform($var){
   ucwords(strtolower(htlmentities(trim($var))));
------------------------^ //It's htmlentities() not htlmentities()
   return $var; //you're just returning the value that is passed to the method
}

You need to get the return value from the PHP methods and return that from your function

function varform($var){
   $var = ucwords(strtolower(htmlentities(trim($var))));
   return $var;
}
彩扇题诗 2024-11-13 21:51:56

1)用htmlentities替换htlmentities

,正如之前的评论者所说

function varform($var){
  return ucwords(strtolower(htmlentities(trim($var))));
}

1) replace htlmentities by htmlentities

and as the previous commenters said

function varform($var){
  return ucwords(strtolower(htmlentities(trim($var))));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文