Format() function in PHP? (conerting pawn to PHP) -
linuxthefish - 06.08.2011
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.
Re: Format() function in PHP? (conerting pawn to PHP) -
Farsek - 06.08.2011
sprintf
http://php.net/manual/en/function.sprintf.php
I think you can use also '.' ( echo exec(' killall -9 '.$svr ); )
Re: Format() function in PHP? (conerting pawn to PHP) -
linuxthefish - 06.08.2011
Quote:
Originally Posted by Farsek
|
I've tried sprintf, but i don't want to print it - i want to use it!
Re: Format() function in PHP? (conerting pawn to PHP) -
SchurmanCQC - 06.08.2011
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'):
EDIT: +rep if this helped, please. <3
Re: Format() function in PHP? (conerting pawn to PHP) -
linuxthefish - 06.08.2011
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'):
EDIT: +rep if this helped, please. <3
|
Thanks, you saved my life
Re: Format() function in PHP? (conerting pawn to PHP) -
kc - 06.08.2011
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!)
Re: Format() function in PHP? (conerting pawn to PHP) -
linuxthefish - 06.08.2011
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
Re: Format() function in PHP? (conerting pawn to PHP) -
Blacklite - 07.08.2011
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."
Re: Format() function in PHP? (conerting pawn to PHP) -
Sasino97 - 07.08.2011
or you can just use the . symbol tht concatenates the strings.