I have a PHP Script that users will enter a name like: Alex_Newton
,
However, some users will use a space rather than an underscore, so my question is:
How do I auto-replace spaces with Underscores in PHP?
I have a PHP Script that users will enter a name like: Alex_Newton
,
However, some users will use a space rather than an underscore, so my question is:
How do I auto-replace spaces with Underscores in PHP?
function fixArrayKey(&$arr)
{
$arr=array_combine(array_map(function($str){return str_replace(" ","_",$str);},array_keys($arr)),array_values($arr));
foreach($arr as $key=>$val)
{
if(is_array($val)) fixArrayKey($arr[$key]);
}
}
Tested as below:
$data=array("key 1"=>"abc","key 2"=>array("sub 1"=>"abc","sub 2"=>"def"),"key 3"=>"ghi");
print_r($data);
fixArrayKey($data);
print_r($data);
This outputs:
Array
(
[key 1] => abc
[key 2] => Array
(
[sub 1] => abc
[sub 2] => def
)
[key 3] => ghi
)
Array
(
[key_1] => abc
[key_2] => Array
(
[sub_1] => abc
[sub_2] => def
)
[key_3] => ghi
)
Here's what worked best for me when trying to script this (in case anyone else comes across this like I did):
$ pecl -d php_suffix=5.6 install <package>
$ pecl uninstall -r <package>
$ pecl -d php_suffix=7.0 install <package>
$ pecl uninstall -r <package>
$ pecl -d php_suffix=7.1 install <package>
$ pecl uninstall -r <package>
The -d php_suffix=<version>
piece allows you to set config values at run time vs pre-setting them with pecl config-set
. The uninstall -r
bit does not actually uninstall it (from the docs):
vagrant@homestead:~$ pecl help uninstall
pecl uninstall [options] [channel/]<package> ...
Uninstalls one or more PEAR packages. More than one package may be
specified at once. Prefix with channel name to uninstall from a
channel not in your default channel (pecl.php.net)
Options:
...
-r, --register-only
do not remove files, only register the packages as not installed
...
The uninstall line is necessary otherwise installing it will remove any previously installed version, even if it was for a different PHP version (ex: Installing an extension for PHP 7.0 would remove the 5.6 version if the package was still registered as installed).
PROBLEM SOLVED:
Found this wonderful article in the meantime. Upon that I just implemented functions from the article, modified Replace in ResolvePropertyName and voila, my code is working splendidly! Hope this helps everyone else who is using Entity Framework, generating model from database and trying to go from C# objects to JSON.
public class JSON
{
public static string ConvertEntityToJSON(object dataToSerialize)
{
return JsonConvert.SerializeObject(dataToSerialize,
new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new UnderscorePropertyNamesContractResolver()
});
}
}
public class UnderscorePropertyNamesContractResolver : DefaultContractResolver
{
public UnderscorePropertyNamesContractResolver() : base()
{
}
protected override string ResolvePropertyName(string propertyName)
{
return Regex.Replace(propertyName, "_", " ");
}
}
Never used any of those, but they look interesting..
Take a look at Gearman as well.. more overhead in systems like these but you get other cool stuff :) Guess it depends on your needs ..