#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Location
    Tulsa, OK
    Posts
    90
    Rep Power
    14

    preg_replace problem with dollar signs ($)


    Hello,
    I ran into an interesting problem today with the preg_replace function. I am using it to replace a part of an HTML document with somehting else, in order to build a table dynamically from database data, like below:

    $htmlPage = '
    <TABLE>
    <!--resultTable-->
    <TR>
    <TD>Temp Text</TD>
    </TR>
    <!--resultTable-->
    </TABLE>';

    while (whatever){
    $htmlInsert .= '
    <TR>
    <TD>'.stripslashes($mysqlText).'</TD>
    </TR>';
    }

    echo preg_replace("/\<\!\-\-resultTable\-\-\>.*?\<\!\-\-resultTable\-\-\>/s", $htmlInsert, $htmlPage);

    Now, the puzzling thing is that if $mysqlText has a dollar sign ($) in it anywhere, like in $4.95 or $0.99 (basically any currency amount), The resulting table will look very strange. Of what I saw, it will make a table with data out of order and the table is all funky looking and the data between the <!--resultTable--> tags int he $htmlPage variable is shown sometimes 5 times in the messed up table.

    I guess what I am asking is...is there any way to make the preg_replace function ignore dollar signs ($)? I have tried escaping the dollar signs by replacing them with \$ and I even tried \\$ and \\\$ and it had no effect at all. I still need my stripslashes function so characters like single quotes (') don't show up like \'.

    I hope I made since here. Thanks a lot for any help.
    -Chris C.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Location
    NYC
    Posts
    79
    Rep Power
    13
    I didn't try these out but perhaps you will consider one of the following:

    1) maybe replace each $ in $htmlInsert with $\ (or $\\\) prior to the replace that you are doing.
    2) If that does not work, replace each % in $htmlInsert with %25 (to avoid any accidental mess ups in the final unreplace) and then each $ in $htmlInsert with %24. Now do your replace function. Now unreplace %24 and %25. If you go this latter route then the first part could be done with protectStr($htmlInsert, "%$", "%", 2);

    using

    function protectStr ($str, $chars, $encDelim, $hexLen) {
    // replaces those chars in $chars with $encDelimHEXCODE within $str
    // don't try to protect 0-9 and A-F. If protecting %, protect it first
    for ($i=0;$i<strlen($chars);$i++)
    $str = str_replace($chars[$i],$encDelim . substr(str_repeat("0",$hexLen) . base_convert("" . ord($chars[$i]),10,16),-$hexLen), $str);
    return $str;
    }

    Good luck,
    Csaba Gabor
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Location
    Tulsa, OK
    Posts
    90
    Rep Power
    14
    Thanks Csaba. I tried replacing all dollar signs ($) with dollar sign backslash zero ($\0) and it worked. The preg_replace function would no longer think currency amounts were variables. Cool.

    -Chris C.

IMN logo majestic logo threadwatch logo seochat tools logo