note 101296 modified in function.preg-replace by thiago
| From: | thiago@php.net | Date: | Wed, 08 Dec 2010 18:36:59 +0000 |
| Subject: | note 101296 modified in function.preg-replace by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-174714@lists.php.net to get a copy of this message | ||
I find it useful to output HTML form names to the user from time to time while going through the
$_GET or $_POST on a user's submission and output keys of the GET or POST array... the only
problem being in the name attribute I follow common programming guidelines and have names like the
following: eventDate, eventTime, userEmail, etc. Not great to just output to the user-- so I came up
with this function. It just adds a space before any uppercase letter in the string.
<?php
function caseSwitchToSpaces( $stringVariableName )
{
$pattern = '/([A-Z])/';
$replacement = ' ${1}';
return preg_replace( $pattern, $replacement, $stringVariableName );
}
//ex.
echo( caseSwitchToSpaces( "helloWorld" ) );
?>
would output:
"hello World"
You could also do title-style casing to it if desired so the first word isn't lowercase.
--was--
I find it useful to output HTML form names to the user from time to time while going through the
$_GET or $_POST on a user's submission and output keys of the GET or POST array... the only
problem being in the name attribute I follow common programming guidelines and have names like the
following: eventDate, eventTime, userEmail, etc. Not great to just output to the user-- so I came up
with this function. It just adds a space before any uppercase letter in the string.
function caseSwitchToSpaces( $stringVariableName )
{
$pattern = '/([A-Z])/';
$replacement = ' ${1}';
return preg_replace( $pattern, $replacement, $stringVariableName );
}
ex. echo( caseSwitchToSpaces( "helloWorld" ) );
would output:
"hello World"
You could also do title-style casing to it if desired so the first word isn't lowercase.
http://php.net/manual/en/function.preg-replace.php