printing a string with % using sprintf

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Venkat Venkataraju

    printing a string with % using sprintf

    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
  • Jay Moore

    #2
    Re: printing a string with % using sprintf

    Double % sign.

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

    -Jay


    Venkat Venkataraju wrote:
    [color=blue]
    > 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[/color]

    Comment

    • Venkat Venkataraju

      #3
      Re: printing a string with % using sprintf


      Jay Moore wrote:[color=blue]
      > Double % sign.
      >
      > sprintf("<table width='100%%' id='%s'>\n", $table_id);
      >[/color]
      doesn't seems to work. it still asks for more parms. i also did \%, but
      it is not working either.

      /V

      Comment

      • Jay Moore

        #4
        Re: printing a string with % using sprintf



        Venkat Venkataraju wrote:
        [color=blue]
        >
        > Jay Moore wrote:
        >[color=green]
        >> Double % sign.
        >>
        >> sprintf("<table width='100%%' id='%s'>\n", $table_id);
        >>[/color]
        > doesn't seems to work. it still asks for more parms. i also did \%, but
        > it is not working either.
        >
        > /V[/color]

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

        -Jay

        Comment

        • Inuyasha4Life

          #5
          Re: printing a string with % using sprintf

          Venkat Venkataraju wrote:
          [color=blue]
          > 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[/color]

          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

          Comment

          • Five Cats

            #6
            Re: printing a string with % using sprintf

            In message <h138c.15249$t1 6.8617070@newss vr28.news.prodi gy.com>, Venkat
            Venkataraju <segfault@?.?.i nvalid> writes[color=blue]
            >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.[/color]

            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

            Comment

            • Michiel Overtoom

              #7
              Re: printing a string with % using sprintf

              Venkat Venkataraju <segfault\"@\"n ospam.sbcglobal .net> wrote:
              [color=blue]
              > line = sprintf("<table width=100% id=%s>\n", $table_id);[/color]

              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.

              Comment

              Working...