1

In python, we can write COMMAND("%s.pdf"%param). How is it possible to do the same in PHP, e.g.

<iframe src="/example/%user_input.pdf"%param width="100%" height="100px"> </iframe>

David Brossard
  • 13,584
  • 6
  • 55
  • 88
User122
  • 25
  • 4
  • 1
    Does this answer your question? [vsprintf or sprintf with named arguments, or simple template parsing in PHP](https://stackoverflow.com/questions/5701985/vsprintf-or-sprintf-with-named-arguments-or-simple-template-parsing-in-php) – steven7mwesigwa Feb 27 '21 at 19:41

2 Answers2

1

You're looking for sprintf. See the PHP manual: https://www.php.net/manual/en/function.sprintf.php

`<?php
$num = 5;
$location = 'tree';

$format = 'There are %d monkeys in the %s';
echo sprintf($format, $num, $location);
?>`
David Brossard
  • 13,584
  • 6
  • 55
  • 88
1

I think the closest to Python 2 named parameter string formatting is strtr:

<?php

$params = [
  '%user_input' => 'hello',
];

?>

<iframe src="<?php print strtr('/example/%user_input.pdf', $params); ?>" width="100%" height="100px"> </iframe>

This function does simple text replacements. It does not supoort variable type hints.

Koala Yeung
  • 7,475
  • 3
  • 30
  • 50