RE: [PHP] Converting a string
| From: | Jay Blanchard | Date: | Fri, 17 Mar 2006 20:07:11 +0000 |
| Subject: | RE: [PHP] Converting a string | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-232159@lists.php.net to get a copy of this message | ||
[snip]
Well, you didn't answer the second question, how would you do
it? So
far I see a pattern: "ignore the lowercase letters at the beginnig and
add a space before an uppercase" (this won't apply to all field names,
and I hope you're aware of that), so try something like
$text = preg_replace('/^[a-z]+\s*/X', '',
preg_replace('/(?=[A-Z])/X', ' ', $name) );
Now, a better way, IMHO, would be to use "_" as spaces and write
the
name of the field as you want it to appear (e.g. "First_Name"), so you
just need to
$text = str_replace('_', ' ', $name);
--pretty much simplier, isn't?
And... if you want to add a prefix (such as "ps") then make it
configurable, so it can be changed anytime without effort.
Last comment: you should think about the question you didn't answer, and
that might give you the solution to your problem.
[/snip]
I was just headed in the wrong direction with preg_split(), and I had it
stuck in my head.
If I had delimiters, such as underscores, this question would have never
come up. I am not in a position to tell the designers that we need them
to change their naming conventions in forms at this unfortunately, so I
need to work with what we have as efficiently as possible.
Thank you for an elegant solution, but I am curious....why would it not
work with all field names if they were named using the conventions
(studly caps)? I can see a problem perhaps with a field named fooMyURL,
it would come out as My U R L. Do you feel as if there are any other
gotcha's?