printing a string with % using sprintf – PHP

  php

Q(Question):

Hi All

I’m having problems printing a string with a % char. Here is the code

line = sprintf("<table width=100% id=%s>\n", $table_id);

I’m getting an not enough parameter error. The % for the 100% is being
mis interepreted as a format char. Though i worked around by coding it
line = sprintf("<table width=100%s id=%s>\n", ‘%’, $table_id);, i think
there should be a way with which the % can be escaped PHP.

The PHP documention does not have any info about this. if anyone has
come accross such problems and found a work around, please help me out.

Thanks
/V

A(Answer):

Double % sign.

sprintf("<table width=’100%%’ id=’%s’>\n", $table_id);

-Jay
Venkat Venkataraju wrote:

Hi All

I’m having problems printing a string with a % char. Here is the code

line = sprintf("<table width=100% id=%s>\n", $table_id);

I’m getting an not enough parameter error. The % for the 100% is being
mis interepreted as a format char. Though i worked around by coding it
line = sprintf("<table width=100%s id=%s>\n", ‘%’, $table_id);, i think
there should be a way with which the % can be escaped PHP.

The PHP documention does not have any info about this. if anyone has
come accross such problems and found a work around, please help me out.

Thanks
/V

A(Answer):

Jay Moore wrote:

Double % sign.

sprintf("<table width=’100%%’ id=’%s’>\n", $table_id);

doesn’t seems to work. it still asks for more parms. i also did \%, but
it is not working either.

/V

A(Answer):

Venkat Venkataraju wrote:

Jay Moore wrote:

Double % sign.

sprintf("<table width=’100%%’ id=’%s’>\n", $table_id);

doesn’t seems to work. it still asks for more parms. i also did \%, but
it is not working either.

/V

Works just fine for me. Make sure you don’t have any typos.

-Jay

A(Answer):

Venkat Venkataraju wrote:

Hi All

I’m having problems printing a string with a % char. Here is the code

line = sprintf("<table width=100% id=%s>\n", $table_id);

I’m getting an not enough parameter error. The % for the 100% is being
mis interepreted as a format char. Though i worked around by coding it
line = sprintf("<table width=100%s id=%s>\n", ‘%’, $table_id);, i think
there should be a way with which the % can be escaped PHP.

The PHP documention does not have any info about this. if anyone has
come accross such problems and found a work around, please help me out.

Thanks
/V

If it’s anything like C++ (as PHP usually is), and if I remember my c++
correctly, it’s going to be %%. Try that.

— Inuyasha4Life

A(Answer):

In message <h1*********************@newssvr28.news.prodigy.co m>, Venkat
Venkataraju <segfault@?.?.invalid> writes

Hi All

I’m having problems printing a string with a % char. Here is the code

line = sprintf("<table width=100% id=%s>\n", $table_id);

I’m getting an not enough parameter error. The % for the 100% is being
mis interepreted as a format char. Though i worked around by coding it
line = sprintf("<table width=100%s id=%s>\n", ‘%’, $table_id);, i think
there should be a way with which the % can be escaped PHP.

The PHP documention does not have any info about this. if anyone has
come accross such problems and found a work around, please help me
out.

You have a percent literal in the line. You can just concatenate it:

line = "<table width=100% id=" . $tableid . ">\n";


Five Cats
Email to: cats_spam at uk2 dot net

A(Answer):

Venkat Venkataraju <segfault\"@\"nospam.sbcglobal.net> wrote:

line = sprintf("<table width=100% id=%s>\n", $table_id);

This will work:

line = sprintf("<table width=100%s id=%s>\n", ‘%’, $table_id);

Spam is spam no matter who’s doing it or for what reason.

LEAVE A COMMENT