PHP 替换和小写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8741918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:23:29  来源:igfitidea点击:

PHP Replace and Lowercase

phpreplace

提问by TheBlackBenzKid

I am trying to do a PHP replace like ASP Replace. The function just returns a blank value though?

我正在尝试进行像 ASP 替换这样的 PHP 替换。该函数只是返回一个空值?

    $zonename=mysql_real_escape_string($_POST["zonename"]);
    $zname_clean =""; # blank string
    $zname_clean = $zonename; # fill string with the post form
    $zname_clean = str_replace($zname_clean, " ", ""); # remove white space

That is my code. Example Zonename would be "Header Left", I want to remove capitalization and also remove whitespace.

那是我的代码。示例 Zonename 将是“Header Left”,我想删除大写并删除空格。

How can I remove whitespace and convert the case and also is there a cleaner way of doing this?

如何删除空格并转换大小写,还有更简洁的方法吗?

回答by Treffynnon

// strip out all whitespace
$zname_clean = preg_replace('/\s*/', '', $zname_clean);
// convert the string to all lowercase
$zname_clean = strtolower($zname_clean);

See the PHP manual for strtolower()and preg_replace().

请参阅PHP手册strtolower()preg_replace()

回答by OptimusCrime

You have your parameters in the wrong order. Take a look at this: http://php.net/manual/en/function.str-replace.php

您的参数顺序错误。看看这个:http: //php.net/manual/en/function.str-replace.php

It should be str_replace(" ", "", $zname_clean);

它应该是 str_replace(" ", "", $zname_clean);

Another way of doing this is strtolower(trim($zname_clean));

另一种方法是 strtolower(trim($zname_clean));

回答by Prasad Rajapaksha

Try this .

尝试这个 。

$zonename=mysql_real_escape_string($_POST["zonename"]);
$zname_clean = strtolower(str_replace(" ", "",$zonename));