PHP sprintf escaping %
Escape it with another %
:
$stringWithVariables = 'About to deduct 50%% of %s %s from your Top-Up account.';
sprintf throws warning with % in string
You need to escape it with another %
:
<table width="100%%"....
Characters and in sprintf()
So what's happening is that your browser (Chrome, IE, Sarafi, etc), thinks that the thing between the brackets is a HTML tag. Try this:
$mail_to = sprintf('%s %s <%s>', 'Harry', 'Potter', 'h.potter@example.com');
See if that works.
php singlequote not working on sprintf properly
you can refer to the manual that specifies that single quotes in php consider most escape sequences as litterals, contrary ot double quotes:
http://php.net/manual/en/language.types.string.php
Escaping in sprintf
There are no standard functions that will do the un-escaping for you, so you will have to do it yourself.
void unescape(const char *in, char *out)
{
int esc = 0;
while (*in) {
if (esc) {
switch(*in) {
case 'r':
*out++ = '\r';
break;
case 'n':
*out++ = '\n';
break;
default:
*out++ = '\\';
*out++ = *in;
break;
}
esc = 0;
} else if (*in == '\\') {
esc = 1;
} else {
*out++ = *in;
}
in++;
}
*out = 0;
}
(*note, the 'out' buffer must be at least as large as the 'in' buffer. And if the last char is a \, it is lost)
You'd then do
char input[BUFSZ];
char buf[BUFSZ];
//read data from the file into 'input'
unescape(input, buf);
sprintf(target, buf, ...);
Adding a \n newline symbol when using sprintf in PHP
The newline symbol \n
will only being interpreted when it is enclosed in double quotes: "
. Like that:
$line = '...............';
$line .= "\n";
You should also know about PHP_EOL
It is a constant that contains the systems newline delimiter which is different on several operating systems. For example it will be \n
on Linux but \r\n
on Windows. The most portable code would look like:
$line = '.....';
$line .= PHP_EOL;
What is the meaning of sprintf(): Too few arguments
You have two %s items in the string but only one parameter supplied after the string. For each '%' item in the format string it expects a matching parameter after the string to use to find in the value.
like this:
sprintf("item 1: %s, item 2: %s", "item1", "item2");
what you have is like:
sprintf("item 1: %s, item 2: %s", "item1");
so there is no entry for the item 2 string to match
vsprintf or sprintf with named arguments, or simple template parsing in PHP
As far as I know printf/sprintf does not accept assoc arrays.
However it is possible to do printf('%1$d %1$d', 1);
Better than nothing ;)
Related Topics
Understanding MVC: Whats the Concept of "Fat" on Models, "Skinny" on Controllers
What Security Issues Should I Look Out for in PHP
Continue Processing After Closing Connection
How to Enable and Use Http Put and Delete with Apache2 and PHP
Setting Up Ssl on a Local Xampp/Apache Server
How to Re-Format Datetime String in PHP
How to Get All Captures of Subgroup Matches with Preg_Match_All()
Pass Data from Jquery to PHP for an Ajax Post
Why Doesn't This Code Simply Print Letters a to Z
Using Usort in PHP with a Class Private Function
Storing Objects in PHP Session
Guzzle: Handle 400 Bad Request
Counting Occurrence of Specific Value in an Array with PHP
MySQL Error "Too Many Connections"
How to Set a Default Attribute Value for a Laravel/Eloquent Model