PHP Sprintf Escaping %

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



Leave a reply



Submit