Is there an equivalent of Python str.format
in PHP?
In Python:
"my {} {} cat".format("red", "fat")
All I see I can do in PHP natively is by naming the entries and using str_replace
:
str_replace(array('{attr1}', '{attr2}'), array('red', 'fat'), 'my {attr1} {attr2} cat')
Is there any other PHP's native alternatives?
sprintf
is the closest thing. It's the old-style Python string formatting:As PHP doesn't really have a proper alternative to
str.format
in Python, I decided to implement my very simple own which as most of the basic functionnalitites of the Python's one.{}
matched will be transformed into{0}
, etc),