How do I convert a dash-delimited string to a PHP namespace format?
-
I need to convert "system/common/pdf-logger" to "System\Common\PdfLogger", which involves a 2 step preg_replace( ). First i'll need to convert all backslashes (/) to forward slashes, and convert the following character to an uppercase (so "system/common/pdf-logger" would become "System\Common\Pdf-logger"). Then I'll need to remove all dashes and convert the following character to an uppercase (so that "System\Common\Pdf-logger" would become "System\Common\PdfLogger"). How do I do that with just one preg_replace( ) call? I can do that with two, but it'll be really nice if I can perform both evaluations in one go.
-
Answer:
Well here's a one line function that does it for you, not pretty but works good :) function create_namespace ($string) { return str_replace (" ", "", str_replace ("/", "\\", ucwords (str_replace ('-', ' ', str_replace ('/ ', '/', ucwords (str_replace ('/', '/ ', $string))))))); }$str = "system/common/pdf-logger";echo create_namespace ($str);
Vince Kronlein at Quora Visit the source
Related Q & A:
- How do I convert a PDF file to PDF/A in Delphi?Best solution by softwarerecs.stackexchange.com
- How do I convert a bitmap to vector?Best solution by Super User
- How can I convert a string number to a number in Perl?Best solution by Stack Overflow
- How can I convert a video to mp3 format?Best solution by Yahoo! Answers
- How can i convert a game on my old pc to my new pc?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.