[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 505
  • Last Modified:

Hyperlinks not formatting correctly using TinyMCE

Hi, I am trying to insert hyperlinks on my pages using TinyMCE.

I have tried both using the toolbar option and adding the link details in there, and also manually, but each time it is outputting wrong.

instead of the link being http://www.numyspace.co.uk/~unn_r031923/Testing/login-OOtest.php

it is being outputted like this.

http://www.numyspace.co.uk/~unn_r031923/Testing/\"login-OOtest.php\"

I have searched the help files and found this below. The question is, how could i adapt this script into my index.php code?



HTML output include lots of \" like <a href=\"mylink.htm\">link</a>?

This is probably because you are using PHP and it has a feature that's called magic quotes that is enabled by default. You can read more about this in the PHP documentation.

A quick way to clean up magic quotes is to use this (taken from the PHP manual):

if (get_magic_quotes_gpc()) {
    function stripslashes_deep($value)
    {
        $value = is_array($value) ?
                    array_map('stripslashes_deep', $value) :
                    stripslashes($value);

        return $value;
    }

    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
    $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
}


index.php
-----------------
<?php
include("header.html");
 
// Get the page name from the query string
$page = $_GET['page'] .'.txt';
if (!$_GET['page']) {
 
  echo("<h1>Page name not specified</h1>\n");
  include("../footer.html");
  exit;
}
 
// This step is checking to see if I have posted. 
// If I have posted the page contents, it opens up the 
// filename indicated by the query string, writes it out, 
// closes the file, shows  the new contents of the file, 
// sources the footer and quits.
if (isset($_POST['page'])) {
  $handle = fopen("../pages/$page", 'w');
  fwrite($handle, $_POST['page']);
  fclose($handle);
  include("../footer.html");
  exit;
}
 
// If I have specified a page that exists, read it into the 
// $text variable. If the page doesn't exist, 
// mention that I am creating a new one at the top of the page, 
// and set $text to an empty paragraph (<p></p>). 
if (file_exists("../pages/$page")) {
  $FILE = fopen("../pages/$page", "rt");
  while (!feof($FILE)) {
    $text .= fgets($FILE);
  }
  fclose($FILE);
} else {
   echo("<h1>New Page: $page</h1>\n");
  $text = "<p></p>";
}
 
 
// JavaScript is usually loaded in the <head> section of a page. 
// But since the <head> section is // in the shared header.html, 
// I don't want to load this up for every page, just the editing page. 
// Because of this, i am putting it in the <body> section. 
// The "init" section is where all the // options are for the editor. 
// "mode: textareas" means that all text areas on the page will become
// TinyMCE WYSIWYG texareas. 
echo <<< EOM
<script language="javascript" type="text/javascript" src="/~unn_r031923/Testing/tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
<script language="javascript" type="text/javascript">
tinyMCE.init({
 
// General options
mode : "textareas",
theme : "advanced",
 
plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,imagemanager,filemanager",
 
// Theme options
theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
 
theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
 
theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|",
 
theme_advanced_buttons4 : "moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,|,insertfile,insertimage",
 
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_resizing : true,
 
// Example content CSS (should be your site CSS)
content_css : "Testing/project.css",
 
// Drop lists for link/image/media/template dialogs
template_external_list_url : "js/template_list.js",
external_link_list_url : "js/link_list.js",
external_image_list_url : "js/image_list.js",
media_external_list_url : "js/media_list.js",
 
 
});
</script>
EOM;
 
// Here I create the form, the textarea and the "Save" button. 
// The "htmlspecialchars($text)" call reads the $text variable,
// and replaces all the special characters with the 
// HTML special character codes. This is so the HTML in 
// the file doesn't clash with the HTML on this page.
// The output of this function then gets passed to the textarea.
// When the "Save" is clicked, it calls the same page again 
// with the same query string, but this time there is some posted
// information containing the editing i did. 
// This is sent to the second section of code at the top
// of this page to be saved to a file.
 
$this_page = $_SERVER['PHP_SELF'];
$query_string = $_SERVER['QUERY_STRING'];
echo("<form method=\"post\" action=\"$this_page?$query_string\">\n");
echo("<textarea id=\"page\" name=\"page\" rows='25' cols='80'>\n");
echo(htmlspecialchars($text));
echo("</textarea>\n");
echo("<input type=\"submit\" value=\"Save\"/>\n");
echo("</form>\n");
 
// load the footer and close out the PHP script.
include("footer.html");
 
?>

Open in new window

0
LearningPHPMySQL
Asked:
LearningPHPMySQL
  • 3
  • 2
1 Solution
 
prokvkCommented:
The script beginning with "if (get_magic_quotes_gpc()) {" should go to your action (form action) script before you use the _POST, _GET or whatever values. You can also use just the function - stripslashes on your frontend in order to get rid of the slashes ..
0
 
LearningPHPMySQLAuthor Commented:
how would i add that to the form please?  

Sorry, I'm a bit thick, still learning : (
0
 
prokvkCommented:
You dont add it to a form .. you process the form in the action script (form action="THIS_IS_THE_SCRIPT_IM_TALKING_ABOUT"). In this script before you add the TinyMCE output to the DB use the code you pasted, or simply function stripslashes($tinyMceCode);

:)
0
 
LearningPHPMySQLAuthor Commented:
Like this ?
if (get_magic_quotes_gpc()) {
function stripslashes_deep($tinyMCE)
    {
        $value = is_array($tinyMCE) ?
                    array_map('stripslashes_deep', $tinyMCE) :
                    stripslashes($tinyMCE);
 
        return $tinyMCE;
    }
    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
    $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
}
    
$this_page = $_SERVER['PHP_SELF'];
$query_string = $_SERVER['QUERY_STRING'];
echo("<form method=\"post\" action=\"$tinyMCE?$tinyMceCode?$this_page?$query_string\">\n");
echo("<textarea id=\"page\" name=\"page\" rows='25' cols='80'>\n");
echo(htmlspecialchars($text));
echo("</textarea>\n");
echo("<input type=\"submit\" value=\"Save\"/>\n");
echo("</form>\n");
 
// load the footer and close out the PHP script.
include("footer.html");
 
?>

Open in new window

0
 
prokvkCommented:
In the line:

echo("<form method=\"post\" action=\"$tinyMCE?$tinyMceCode?$this_page?$query_string\">\n");

there's the action, I don't know from this what the action exactly is, but you have to find that script file and put it there ... Consider also the alternative I gave you to change the link values only on frontend, it can be easier for you. Just run your content variable with TinyMCE (the one that contains "bad" links) through the stripslashes function like this:

$cont = stripslashes($cont);

This should work! :)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now