Jump to content

PhP, MySQL, JScript and accents/quotes


UltraKnorrie

Recommended Posts

Hello,

yes this has already been posted and answered in parts, but so far, I always failed to get the whole picture right.

What's the problem ? Well I've to store, edit and display french texts in a MySQL-database and on a website.
Such french texts contains lots of characters with accents (HTML-codes: ê è ....) and more of that nice stuff, and contain also single and double quotes (and sometimes ampersands too ;-)

So, my website runs PhP; getting the code right to store it in MySQL is no problem, I'm using the function "qesc()" to format the code for proper storing in MySQL:

function qesc($value) {
if (get_magic_quotes_gpc())
$value = stripslashes($value);
// Quote if not integer
if (!is_numeric($value))
$value = mysql_real_escape_string($value);
return $value;
}

But retrieved data must be both being displayed on a website and using popups which are made using javascript and which contain html-formatting tags....
And here I fail to get it working.
What have I done exactly: I "escaped" the strings to be forwarded to the HTML-display using the function javascript_escape(). This one guarantees me a proper handling of the quotes, changing some special characters in such a way that they are no problem to pass them as an argument to the javascript that does make the actual popup appear:

function javascript_escape($str) {
$str = preg_replace("/(\r?\n)|\r/","<br>", nl2br($str));
$str = htmlspecialchars(addslashes($str), ENT_COMPAT, 'ISO8859-15');
return($str);
}

The call for the popup is:

function InsertFixPopup() {
// First argument: title
// Second argument = style (0 = paragraphs, 1 = bullets, 2 = paragraphs, but third argument is hyperlink-tekst, 4 = bullets, but third argument is hyperlink-tekst)
// (Third argument is hyperlink-text)
// 3 (or 4)..n args: text-items
//$tstr = "<?php QuickSessionStarter(); ?> <body background=\'" . $_SESSION['HBdyC'] . "\'>";
$tstr = "<html><body>";
$title= func_get_arg(0);
$typelist= func_get_arg(1);
$linktxt = "...";
switch ($typelist) {
case 0:
$head = "dl";
$shead = "dt";
$bullets = "dd";
$i = 2;

// and some more code that is not important here for a good understanding

$tstr .= "<$head> <b> $title: </b> </$shead>";
$tstr = "Actual text to be displayed, preceeded and followed by some html formatting";
$tstr .= "<br> <br> <br> <center> <input type=\'submit\' class=\'std\' OnClick=\"window.close();\" value=\'" .
$cmdtxt . "\' > </center>";
$tstr .= "</body></html>";
// Now we first properly format the string to make it suitable for JS use: no \n linebreaks ...
$ntstr = javascript_escape($tstr);
return("<a href='#' OnClick=\"ShowStaticPopup('$ntstr',400,300,'yes');return false;\">$linktxt</a>");

Using these kind of escaping etc, I managed to place proper links on my webpages. However, the popup shows the used HTML tags too instead of interpreting them ... I tried also other ways of escaping, but then I got a proper popup, but improper links displaying quotes etc ....
Finally, I gave up not knowing how to find one unifying approach that makes my strings parseable by both php and JScript (and eventually MySQL, although I can live with a function that sets everything right for "only" PhP and JScript.) Oh, yes, one last detail: everything should work both on Windows/Linux and IE6/FireFox ...

So, I'll be very glad if one can offer a helping hand on how to encode/decode php strings containing characters with accents and quotes in such a way that they are displayed properly by PhP AND by JScript (that's being called by PhP) ...

If you can send your answers also to pdemaziere at gmail dot com, I would be very pleased ...

thx in advance,
Patrick
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.