Format() function in PHP? (conerting pawn to PHP)
#1

Hiya, i need to format a string for php, like ("Your name is %s", Bob) would give "Your name is Bob".

Thanks

EDIT: this might help you inderstand, it's what i'm doing

PHP код:
<?php
include 'config.php';
echo 
exec(' killall -9 $svr ');
echo 
"<meta http-equiv=\"refresh\" content=\"1; url=index.html\" />\n";
?>
config.php makes $svr = samp03svr. And i know it works, as i've included and used it in other scripts.
Reply
#2

sprintf
http://php.net/manual/en/function.sprintf.php
I think you can use also '.' ( echo exec(' killall -9 '.$svr ); )
Reply
#3

Quote:
Originally Posted by Farsek
Посмотреть сообщение
sprintf
http://php.net/manual/en/function.sprintf.php
I think you can use also '.' ( echo exec(' killall -9 '.$svr ); )
I've tried sprintf, but i don't want to print it - i want to use it!
Reply
#4

You don't need a format function.

Just use somethin like the following:

PHP код:

<?php
$string 
"Your name is " $nameString ".";
?>
Would return (if nameString contained 'Bob'):

Код:
Your name is Bob.
EDIT: +rep if this helped, please. <3
Reply
#5

Quote:
Originally Posted by Schurman
Посмотреть сообщение
You don't need a format function.

Just use somethin like the following:

PHP код:

<?php
$string 
"Your name is " $nameString ".";
?>
Would return (if nameString contained 'Bob'):

Код:
Your name is Bob.
EDIT: +rep if this helped, please. <3
Thanks, you saved my life
Reply
#6

In the vast majority of cases sprintf is overkill, and PHP offers alternative methods:

PHP allows for variables to be parsed inside strings (like in your example) with double quotes ("), but not single quotes like you are using.

Another method is to use the concatenation operator (full stop) like Farsek suggested.

(I also hope that $svr is sanitized if it is based on any kind of user input!)
Reply
#7

Quote:
Originally Posted by kc
Посмотреть сообщение
In the vast majority of cases sprintf is overkill, and PHP offers alternative methods:

PHP allows for variables to be parsed inside strings (like in your example) with double quotes ("), but not single quotes like you are using.
I never actually knew i could use " in PHP, thanks for that!

Quote:
Originally Posted by kc
Посмотреть сообщение
(I also hope that $svr is sanitized if it is based on any kind of user input!)
It's not, $svr is definable in the config file
Reply
#8

Quote:
Originally Posted by linuxthefish
Посмотреть сообщение
I've tried sprintf, but i don't want to print it - i want to use it!
I know this has already been solved, but sprintf RETURNS the value, it doesn't print it:

http://www.php.net/manual/en/function.sprintf.php

"Returns a string produced according to the formatting string format."
Reply
#9

or you can just use the . symbol tht concatenates the strings.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)