I am trying to do a PHP replace like ASP Replace. The function just returns a blank value though?
$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.
How can I remove whitespace and convert the case and also is there a cleaner way of doing this?
The strtolower() function converts a string to lowercase. Note: This function is binary-safe. Related functions: strtoupper() - converts a string to uppercase.
The str_replace() function replaces some characters with some other characters in a string. This function works by the following rules: If the string to be searched is an array, it returns an array. If the string to be searched is an array, find and replace is performed with every array element.
strtolower() - It converts a string into uppercase. lcfirst() - It converts the first character of a string into lowercase. ucfirst() - It converts the first character of a string into uppercase. ucwords() - It converts the first character of each word in a string into uppercase.
Java String toLowerCase() Method The toLowerCase() method converts a string to lower case letters. Note: The toUpperCase() method converts a string to upper case letters.
// 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()
.
You have your parameters in the wrong order. Take a look at this: http://php.net/manual/en/function.str-replace.php
It should be str_replace(" ", "", $zname_clean);
Another way of doing this is strtolower(trim($zname_clean));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With