Hi,
I'm look for a way to convert strings to php variable names. So I need to delete all characters that can not be used in php vars and I need to replace spaces with underscores.
Example:
String "No." needs to be converted to "no".
String "Phone No." need to be converted to "phone_no".
ect...
Could somebody please help me with this?
Thanks,
Kind regards
Emiel Romein
The Netherlands
Convert strings to var-compatible values
Moderator: General Moderators
php will take uppercase letters. I believe it will also take spaces in indices.
e.g. $foo['Example Key']
in any case, you'll simply need the following:
e.g. $foo['Example Key']
in any case, you'll simply need the following:
Code: Select all
$string = str_replace(' ','_',strtolower($string));Thing is I'm using Smarty (smarty.pear.net).
In Smarty I need to define an array like foo['bar'] as foo.bar.
If my array index = "Phone No." then in Smarty this would look like foo.Phone No. As you can imagen, this will end up in a synax error. So I need to convert the index names to, in this case, phone_no dso I can use foo.phone_no in Smarty.
Hope I'm clear enough.
In Smarty I need to define an array like foo['bar'] as foo.bar.
If my array index = "Phone No." then in Smarty this would look like foo.Phone No. As you can imagen, this will end up in a synax error. So I need to convert the index names to, in this case, phone_no dso I can use foo.phone_no in Smarty.
Hope I'm clear enough.