Jump to content


PhP, MySQL, JScript and accents/quotes

  • Please log in to reply
No replies to this topic

#1 UltraKnorrie

  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 10 March 2006 - 01:04 PM


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');

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,

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users