Is there a way in PHP to name my specifiers like in Python?
I want this in PHP:
$foo = array('name' => 24);
printf("%(name)d", $foo);
I couldn’t find nothing related on google or in the php manual.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Nice question!
You can roll your own without too much trouble by working with regexes. I based the implementation on the idea of calling
vsprintf, which is closest to the stated goal among the built-inprintffamily of functions:To test:
Update: My initial implementation was very lambda-happy. Turns out a super basic
foreachwill suffice, which also makes the function usable in PHP >= 4.1 (not 100% sure about the specific version, but should be around there).See it in action.